1 /*
2 * Copyright (c) 1994, 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
956 * <pre>
957 * new Double(d1).compareTo(new Double(d2))
958 * </pre>
959 *
960 * @param d1 the first {@code double} to compare
961 * @param d2 the second {@code double} to compare
962 * @return the value {@code 0} if {@code d1} is
963 * numerically equal to {@code d2}; a value less than
964 * {@code 0} if {@code d1} is numerically less than
965 * {@code d2}; and a value greater than {@code 0}
966 * if {@code d1} is numerically greater than
967 * {@code d2}.
968 * @since 1.4
969 */
970 public static int compare(double d1, double d2) {
971 if (d1 < d2)
972 return -1; // Neither val is NaN, thisVal is smaller
973 if (d1 > d2)
974 return 1; // Neither val is NaN, thisVal is larger
975
976 long thisBits = Double.doubleToLongBits(d1);
977 long anotherBits = Double.doubleToLongBits(d2);
978
979 return (thisBits == anotherBits ? 0 : // Values are equal
980 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
981 1)); // (0.0, -0.0) or (NaN, !NaN)
982 }
983
984 /** use serialVersionUID from JDK 1.0.2 for interoperability */
985 private static final long serialVersionUID = -9172774392245257468L;
986 }
|
1 /*
2 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
956 * <pre>
957 * new Double(d1).compareTo(new Double(d2))
958 * </pre>
959 *
960 * @param d1 the first {@code double} to compare
961 * @param d2 the second {@code double} to compare
962 * @return the value {@code 0} if {@code d1} is
963 * numerically equal to {@code d2}; a value less than
964 * {@code 0} if {@code d1} is numerically less than
965 * {@code d2}; and a value greater than {@code 0}
966 * if {@code d1} is numerically greater than
967 * {@code d2}.
968 * @since 1.4
969 */
970 public static int compare(double d1, double d2) {
971 if (d1 < d2)
972 return -1; // Neither val is NaN, thisVal is smaller
973 if (d1 > d2)
974 return 1; // Neither val is NaN, thisVal is larger
975
976 // Cannot use doubleToRawLongBits because of possibility of NaNs.
977 long thisBits = Double.doubleToLongBits(d1);
978 long anotherBits = Double.doubleToLongBits(d2);
979
980 return (thisBits == anotherBits ? 0 : // Values are equal
981 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
982 1)); // (0.0, -0.0) or (NaN, !NaN)
983 }
984
985 /** use serialVersionUID from JDK 1.0.2 for interoperability */
986 private static final long serialVersionUID = -9172774392245257468L;
987 }
|