1
1
/*
2
- * Copyright (c) 1994, 2020 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 1994, 2021 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
52
52
* use instances for synchronization, or unpredictable behavior may
53
53
* occur. For example, in a future release, synchronization may fail.
54
54
*
55
+ * <h2><a id=equivalenceRelation>Floating-point Equality, Equivalence,
56
+ * and Comparison</a></h2>
57
+ *
58
+ * IEEE 754 floating-point values include finite nonzero values,
59
+ * signed zeros ({@code +0.0} and {@code -0.0}), signed infinities
60
+ * {@linkplain Double#POSITIVE_INFINITY positive infinity} and
61
+ * {@linkplain Double#NEGATIVE_INFINITY negative infinity}), and
62
+ * {@linkplain Double#NaN NaN} (not-a-number).
63
+ *
64
+ * <p>An <em>equivalence relation</em> on a set of values is a boolean
65
+ * relation on pairs of values that is reflexive, symmetric, and
66
+ * transitive. For more discussion of equivalence relations and object
67
+ * equality, see the {@link Object#equals Object.equals}
68
+ * specification. An equivalence relation partitions the values it
69
+ * operates over into sets called <i>equivalence classes</i>. All the
70
+ * members of the equivalence class are equal to each other under the
71
+ * relation. An equivalence class may contain only a single member. At
72
+ * least for some purposes, all the members of an equivalence class
73
+ * are substitutable for each other. In particular, in a numeric
74
+ * expression equivalent values can be <em>substituted</em> for one
75
+ * another without changing the result of the expression, meaning
76
+ * changing the equivalence class of the result of the expression.
77
+ *
78
+ * <p>Notably, the built-in {@code ==} operation on floating-point
79
+ * values is <em>not</em> an equivalence relation. Despite not
80
+ * defining an equivalence relation, the semantics of the IEEE 754
81
+ * {@code ==} operator were deliberately designed to meet other needs
82
+ * of numerical computation. There are two exceptions where the
83
+ * properties of an equivalence relation are not satisfied by {@code
84
+ * ==} on floating-point values:
85
+ *
86
+ * <ul>
87
+ *
88
+ * <li>If {@code v1} and {@code v2} are both NaN, then {@code v1
89
+ * == v2} has the value {@code false}. Therefore, for two NaN
90
+ * arguments the <em>reflexive</em> property of an equivalence
91
+ * relation is <em>not</em> satisfied by the {@code ==} operator.
92
+ *
93
+ * <li>If {@code v1} represents {@code +0.0} while {@code v2}
94
+ * represents {@code -0.0}, or vice versa, then {@code v1 == v2} has
95
+ * the value {@code true} even though {@code +0.0} and {@code -0.0}
96
+ * are distinguishable under various floating-point operations. For
97
+ * example, {@code 1.0/+0.0} evaluates to positive infinity while
98
+ * {@code 1.0/-0.0} evaluates to <em>negative</em> infinity and
99
+ * positive infinity and negative infinity are neither equal to each
100
+ * other nor equivalent to each other. Thus, while a signed zero input
101
+ * most commonly determines the sign of a zero result, because of
102
+ * dividing by zero, {@code +0.0} and {@code -0.0} may not be
103
+ * substituted for each other in general. The sign of a zero input
104
+ * also has a non-substitutable effect on the result of some math
105
+ * library methods.
106
+ *
107
+ * </ul>
108
+ *
109
+ * <p>For ordered comparisons using the built-in comparison operators
110
+ * ({@code <}, {@code <=}, etc.), NaN values have another anomalous
111
+ * situation: a NaN is neither less than, nor greater than, nor equal
112
+ * to any value, including itself. This means the <i>trichotomy of
113
+ * comparison</i> does <em>not</em> hold.
114
+ *
115
+ * <p>To provide the appropriate semantics for {@code equals} and
116
+ * {@code compareTo} methods, those methods cannot simply be wrappers
117
+ * around {@code ==} or ordered comparison operations. Instead, {@link
118
+ * Double#equals equals} defines NaN arguments to be equal to each
119
+ * other and defines {@code +0.0} to <em>not</em> be equal to {@code
120
+ * -0.0}, restoring reflexivity. For comparisons, {@link
121
+ * Double#compareTo compareTo} defines a total order where {@code
122
+ * -0.0} is less than {@code +0.0} and where a NaN is equal to itself
123
+ * and considered greater than positive infinity.
124
+ *
125
+ * <p>The operational semantics of {@code equals} and {@code
126
+ * compareTo} are expressed in terms of {@linkplain #doubleToLongBits
127
+ * bit-wise converting} the floating-point values to integral values.
128
+ *
129
+ * <p>The <em>natural ordering</em> implemented by {@link #compareTo
130
+ * compareTo} is {@linkplain Comparable consistent with equals}. That
131
+ * is, two objects are reported as equal by {@code equals} if and only
132
+ * if {@code compareTo} on those objects returns zero.
133
+ *
134
+ * <p>The adjusted behaviors defined for {@code equals} and {@code
135
+ * compareTo} allow instances of wrapper classes to work properly with
136
+ * conventional data structures. For example, defining NaN
137
+ * values to be {@code equals} to one another allows NaN to be used as
138
+ * an element of a {@link java.util.HashSet HashSet} or as the key of
139
+ * a {@link java.util.HashMap HashMap}. Similarly, defining {@code
140
+ * compareTo} as a total ordering, including {@code +0.0}, {@code
141
+ * -0.0}, and NaN, allows instances of wrapper classes to be used as
142
+ * elements of a {@link java.util.SortedSet SortedSet} or as keys of a
143
+ * {@link java.util.SortedMap SortedMap}.
144
+ *
145
+ * @jls 4.2.3 Floating-Point Types, Formats, and Values
146
+ * @jls 4.2.4. Floating-Point Operations
147
+ * @jls 15.21.1 Numerical Equality Operators == and !=
148
+ * @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}
149
+ *
55
150
* @author Lee Boynton
56
151
* @author Arthur van Hoff
57
152
* @author Joseph D. Darcy
@@ -797,33 +892,18 @@ public static int hashCode(double value) {
797
892
* #doubleToLongBits(double)} returns the identical
798
893
* {@code long} value when applied to each.
799
894
*
800
- * <p>Note that in most cases, for two instances of class
801
- * {@code Double}, {@code d1} and {@code d2}, the
802
- * value of {@code d1.equals(d2)} is {@code true} if and
803
- * only if
804
- *
805
- * <blockquote>
806
- * {@code d1.doubleValue() == d2.doubleValue()}
807
- * </blockquote>
895
+ * @apiNote
896
+ * This method is defined in terms of {@link
897
+ * #doubleToLongBits(double)} rather than the {@code ==} operator
898
+ * on {@code double} values since the {@code ==} operator does
899
+ * <em>not</em> define an equivalence relation and to satisfy the
900
+ * {@linkplain Object#equals equals contract} an equivalence
901
+ * relation must be implemented; see <a
902
+ * href="#equivalenceRelation">this discussion</a> for details of
903
+ * floating-point equality and equivalence.
808
904
*
809
- * <p>also has the value {@code true}. However, there are two
810
- * exceptions:
811
- * <ul>
812
- * <li>If {@code d1} and {@code d2} both represent
813
- * {@code Double.NaN}, then the {@code equals} method
814
- * returns {@code true}, even though
815
- * {@code Double.NaN==Double.NaN} has the value
816
- * {@code false}.
817
- * <li>If {@code d1} represents {@code +0.0} while
818
- * {@code d2} represents {@code -0.0}, or vice versa,
819
- * the {@code equal} test has the value {@code false},
820
- * even though {@code +0.0==-0.0} has the value {@code true}.
821
- * </ul>
822
- * This definition allows hash tables to operate properly.
823
- * @param obj the object to compare with.
824
- * @return {@code true} if the objects are the same;
825
- * {@code false} otherwise.
826
905
* @see java.lang.Double#doubleToLongBits(double)
906
+ * @jls 15.21.1 Numerical Equality Operators == and !=
827
907
*/
828
908
public boolean equals (Object obj ) {
829
909
return (obj instanceof Double )
@@ -975,23 +1055,31 @@ public static long doubleToLongBits(double value) {
975
1055
public static native double longBitsToDouble (long bits );
976
1056
977
1057
/**
978
- * Compares two {@code Double} objects numerically. There
979
- * are two ways in which comparisons performed by this method
980
- * differ from those performed by the Java language numerical
981
- * comparison operators ({@code <, <=, ==, >=, >})
982
- * when applied to primitive {@code double} values:
983
- * <ul><li>
984
- * {@code Double.NaN} is considered by this method
985
- * to be equal to itself and greater than all other
986
- * {@code double} values (including
987
- * {@code Double.POSITIVE_INFINITY}).
988
- * <li>
989
- * {@code 0.0d} is considered by this method to be greater
990
- * than {@code -0.0d}.
1058
+ * Compares two {@code Double} objects numerically.
1059
+ *
1060
+ * This method imposes a total order on {@code Double} objects
1061
+ * with two differences compared to the incomplete order defined by
1062
+ * the Java language numerical comparison operators ({@code <, <=,
1063
+ * ==, >=, >}) on {@code double} values.
1064
+ *
1065
+ * <ul><li> A NaN is <em>unordered</em> with respect to other
1066
+ * values and unequal to itself under the comparison
1067
+ * operators. This method chooses to define {@code
1068
+ * Double.NaN} to be equal to itself and greater than all
1069
+ * other {@code double} values (including {@code
1070
+ * Double.POSITIVE_INFINITY}).
1071
+ *
1072
+ * <li> Positive zero and negative zero compare equal
1073
+ * numerically, but are distinct and distinguishable values.
1074
+ * This method chooses to define positive zero ({@code +0.0d}),
1075
+ * to be greater than negative zero ({@code -0.0d}).
991
1076
* </ul>
992
- * This ensures that the <i>natural ordering</i> of
993
- * {@code Double} objects imposed by this method is <i>consistent
994
- * with equals</i>.
1077
+
1078
+ * This ensures that the <i>natural ordering</i> of {@code Double}
1079
+ * objects imposed by this method is <i>consistent with
1080
+ * equals</i>; see <a href="#equivalenceRelation">this
1081
+ * discussion</a> for details of floating-point comparison and
1082
+ * ordering.
995
1083
*
996
1084
* @param anotherDouble the {@code Double} to be compared.
997
1085
* @return the value {@code 0} if {@code anotherDouble} is
@@ -1002,6 +1090,7 @@ public static long doubleToLongBits(double value) {
1002
1090
* {@code Double} is numerically greater than
1003
1091
* {@code anotherDouble}.
1004
1092
*
1093
+ * @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}
1005
1094
* @since 1.2
1006
1095
*/
1007
1096
public int compareTo (Double anotherDouble ) {
0 commit comments