src/share/classes/java/lang/Double.java

Print this page




1004      *          {@code d2}; and a value greater than {@code 0}
1005      *          if {@code d1} is numerically greater than
1006      *          {@code d2}.
1007      * @since 1.4
1008      */
1009     public static int compare(double d1, double d2) {
1010         if (d1 < d2)
1011             return -1;           // Neither val is NaN, thisVal is smaller
1012         if (d1 > d2)
1013             return 1;            // Neither val is NaN, thisVal is larger
1014 
1015         // Cannot use doubleToRawLongBits because of possibility of NaNs.
1016         long thisBits    = Double.doubleToLongBits(d1);
1017         long anotherBits = Double.doubleToLongBits(d2);
1018 
1019         return (thisBits == anotherBits ?  0 : // Values are equal
1020                 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1021                  1));                          // (0.0, -0.0) or (NaN, !NaN)
1022     }
1023 







































1024     /** use serialVersionUID from JDK 1.0.2 for interoperability */
1025     private static final long serialVersionUID = -9172774392245257468L;
1026 }


1004      *          {@code d2}; and a value greater than {@code 0}
1005      *          if {@code d1} is numerically greater than
1006      *          {@code d2}.
1007      * @since 1.4
1008      */
1009     public static int compare(double d1, double d2) {
1010         if (d1 < d2)
1011             return -1;           // Neither val is NaN, thisVal is smaller
1012         if (d1 > d2)
1013             return 1;            // Neither val is NaN, thisVal is larger
1014 
1015         // Cannot use doubleToRawLongBits because of possibility of NaNs.
1016         long thisBits    = Double.doubleToLongBits(d1);
1017         long anotherBits = Double.doubleToLongBits(d2);
1018 
1019         return (thisBits == anotherBits ?  0 : // Values are equal
1020                 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1021                  1));                          // (0.0, -0.0) or (NaN, !NaN)
1022     }
1023 
1024     /**
1025      * Adds two {@code double} values together as per the + operator.
1026      *
1027      * @param a the first operand
1028      * @param b the second operand
1029      * @return the sum of {@code a} and {@code b}
1030      * @see java.util.function.BinaryOperator
1031      * @since 1.8
1032      */
1033     public static double sum(double a, double b) {
1034         return a + b;
1035     }
1036 
1037     /**
1038      * Returns the greater of two {@code double} values.
1039      *
1040      * @param a the first operand
1041      * @param b the second operand
1042      * @return the larger of {@code a} and {@code b}
1043      * @see java.util.function.BinaryOperator
1044      * @since 1.8
1045      */
1046     public static double max(double a, double b) {
1047         return Math.max(a, b);
1048     }
1049 
1050     /**
1051      * Returns the lesser of two {@code double} values.
1052      *
1053      * @param a the first operand
1054      * @param b the second operand
1055      * @return the lesser of {@code a} and {@code b}.
1056      * @see java.util.function.BinaryOperator
1057      * @since 1.8
1058      */
1059     public static double min(double a, double b) {
1060         return Math.min(a, b);
1061     }
1062 
1063     /** use serialVersionUID from JDK 1.0.2 for interoperability */
1064     private static final long serialVersionUID = -9172774392245257468L;
1065 }