Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
/ lanai Public archive

Commit 474dba2

Browse files
committedFeb 2, 2021
8257086: Clarify differences between {Float, Double}.equals and ==
Reviewed-by: smarks, bpb
1 parent 54e7a64 commit 474dba2

File tree

2 files changed

+174
-81
lines changed

2 files changed

+174
-81
lines changed
 

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

+131-42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -52,6 +52,101 @@
5252
* use instances for synchronization, or unpredictable behavior may
5353
* occur. For example, in a future release, synchronization may fail.
5454
*
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+
*
55150
* @author Lee Boynton
56151
* @author Arthur van Hoff
57152
* @author Joseph D. Darcy
@@ -797,33 +892,18 @@ public static int hashCode(double value) {
797892
* #doubleToLongBits(double)} returns the identical
798893
* {@code long} value when applied to each.
799894
*
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.
808904
*
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.
826905
* @see java.lang.Double#doubleToLongBits(double)
906+
* @jls 15.21.1 Numerical Equality Operators == and !=
827907
*/
828908
public boolean equals(Object obj) {
829909
return (obj instanceof Double)
@@ -975,23 +1055,31 @@ public static long doubleToLongBits(double value) {
9751055
public static native double longBitsToDouble(long bits);
9761056

9771057
/**
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}).
9911076
* </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.
9951083
*
9961084
* @param anotherDouble the {@code Double} to be compared.
9971085
* @return the value {@code 0} if {@code anotherDouble} is
@@ -1002,6 +1090,7 @@ public static long doubleToLongBits(double value) {
10021090
* {@code Double} is numerically greater than
10031091
* {@code anotherDouble}.
10041092
*
1093+
* @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}
10051094
* @since 1.2
10061095
*/
10071096
public int compareTo(Double anotherDouble) {

‎src/java.base/share/classes/java/lang/Float.java

+43-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -51,6 +51,14 @@
5151
* use instances for synchronization, or unpredictable behavior may
5252
* occur. For example, in a future release, synchronization may fail.
5353
*
54+
* <h2><a id=equivalenceRelation>Floating-point Equality, Equivalence,
55+
* and Comparison</a></h2>
56+
*
57+
* The class {@code java.lang.Double} has a <a
58+
* href="Double.html#equivalenceRelation">discussion of equality,
59+
* equivalence, and comparison of floating-point values</a> that is
60+
* equality applicable to {@code float} values.
61+
*
5462
* @author Lee Boynton
5563
* @author Arthur van Hoff
5664
* @author Joseph D. Darcy
@@ -711,33 +719,21 @@ public static int hashCode(float value) {
711719
* returns the identical {@code int} value when applied to
712720
* each.
713721
*
714-
* <p>Note that in most cases, for two instances of class
715-
* {@code Float}, {@code f1} and {@code f2}, the value
716-
* of {@code f1.equals(f2)} is {@code true} if and only if
717-
*
718-
* <blockquote><pre>
719-
* f1.floatValue() == f2.floatValue()
720-
* </pre></blockquote>
721-
*
722-
* <p>also has the value {@code true}. However, there are two exceptions:
723-
* <ul>
724-
* <li>If {@code f1} and {@code f2} both represent
725-
* {@code Float.NaN}, then the {@code equals} method returns
726-
* {@code true}, even though {@code Float.NaN==Float.NaN}
727-
* has the value {@code false}.
728-
* <li>If {@code f1} represents {@code +0.0f} while
729-
* {@code f2} represents {@code -0.0f}, or vice
730-
* versa, the {@code equal} test has the value
731-
* {@code false}, even though {@code 0.0f==-0.0f}
732-
* has the value {@code true}.
733-
* </ul>
734-
*
735-
* This definition allows hash tables to operate properly.
722+
* @apiNote
723+
* This method is defined in terms of {@link
724+
* #floatToIntBits(float)} rather than the {@code ==} operator on
725+
* {@code float} values since the {@code ==} operator does
726+
* <em>not</em> define an equivalence relation and to satisfy the
727+
* {@linkplain Object#equals equals contract} an equivalence
728+
* relation must be implemented; see <a
729+
* href="Double.html#equivalenceRelation">this discussion</a> for
730+
* details of floating-point equality and equivalence.
736731
*
737732
* @param obj the object to be compared
738733
* @return {@code true} if the objects are the same;
739734
* {@code false} otherwise.
740735
* @see java.lang.Float#floatToIntBits(float)
736+
* @jls 15.21.1 Numerical Equality Operators == and !=
741737
*/
742738
public boolean equals(Object obj) {
743739
return (obj instanceof Float)
@@ -884,24 +880,32 @@ public static int floatToIntBits(float value) {
884880
public static native float intBitsToFloat(int bits);
885881

886882
/**
887-
* Compares two {@code Float} objects numerically. There are
888-
* two ways in which comparisons performed by this method differ
889-
* from those performed by the Java language numerical comparison
890-
* operators ({@code <, <=, ==, >=, >}) when
891-
* applied to primitive {@code float} values:
892-
*
893-
* <ul><li>
894-
* {@code Float.NaN} is considered by this method to
895-
* be equal to itself and greater than all other
896-
* {@code float} values
897-
* (including {@code Float.POSITIVE_INFINITY}).
898-
* <li>
899-
* {@code 0.0f} is considered by this method to be greater
900-
* than {@code -0.0f}.
883+
* Compares two {@code Float} objects numerically.
884+
*
885+
* This method imposes a total order on {@code Float} objects
886+
* with two differences compared to the incomplete order defined by
887+
* the Java language numerical comparison operators ({@code <, <=,
888+
* ==, >=, >}) on {@code float} values.
889+
*
890+
* <ul><li> A NaN is <em>unordered</em> with respect to other
891+
* values and unequal to itself under the comparison
892+
* operators. This method chooses to define {@code
893+
* Float.NaN} to be equal to itself and greater than all
894+
* other {@code double} values (including {@code
895+
* Float.POSITIVE_INFINITY}).
896+
*
897+
* <li> Positive zero and negative zero compare equal
898+
* numerically, but are distinct and distinguishable values.
899+
* This method chooses to define positive zero ({@code +0.0f}),
900+
* to be greater than negative zero ({@code -0.0f}).
901901
* </ul>
902902
*
903903
* This ensures that the <i>natural ordering</i> of {@code Float}
904-
* objects imposed by this method is <i>consistent with equals</i>.
904+
* objects imposed by this method is <i>consistent with
905+
* equals</i>; see <a href="Double.html#equivalenceRelation">this
906+
* discussion</a> for details of floating-point comparison and
907+
* ordering.
908+
*
905909
*
906910
* @param anotherFloat the {@code Float} to be compared.
907911
* @return the value {@code 0} if {@code anotherFloat} is
@@ -912,8 +916,8 @@ public static int floatToIntBits(float value) {
912916
* {@code Float} is numerically greater than
913917
* {@code anotherFloat}.
914918
*
919+
* @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}
915920
* @since 1.2
916-
* @see Comparable#compareTo(Object)
917921
*/
918922
public int compareTo(Float anotherFloat) {
919923
return Float.compare(value, anotherFloat.value);

0 commit comments

Comments
 (0)
This repository has been archived.