8257086: Clarify differences between {Float, Double}.equals and ==

Reviewed-by: smarks, bpb
This commit is contained in:
Joe Darcy 2021-02-02 02:33:56 +00:00
parent 54e7a642bb
commit 474dba2d8b
2 changed files with 173 additions and 80 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -51,6 +51,14 @@ import jdk.internal.vm.annotation.IntrinsicCandidate;
* use instances for synchronization, or unpredictable behavior may
* occur. For example, in a future release, synchronization may fail.
*
* <h2><a id=equivalenceRelation>Floating-point Equality, Equivalence,
* and Comparison</a></h2>
*
* The class {@code java.lang.Double} has a <a
* href="Double.html#equivalenceRelation">discussion of equality,
* equivalence, and comparison of floating-point values</a> that is
* equality applicable to {@code float} values.
*
* @author Lee Boynton
* @author Arthur van Hoff
* @author Joseph D. Darcy
@ -711,33 +719,21 @@ public final class Float extends Number
* returns the identical {@code int} value when applied to
* each.
*
* <p>Note that in most cases, for two instances of class
* {@code Float}, {@code f1} and {@code f2}, the value
* of {@code f1.equals(f2)} is {@code true} if and only if
*
* <blockquote><pre>
* f1.floatValue() == f2.floatValue()
* </pre></blockquote>
*
* <p>also has the value {@code true}. However, there are two exceptions:
* <ul>
* <li>If {@code f1} and {@code f2} both represent
* {@code Float.NaN}, then the {@code equals} method returns
* {@code true}, even though {@code Float.NaN==Float.NaN}
* has the value {@code false}.
* <li>If {@code f1} represents {@code +0.0f} while
* {@code f2} represents {@code -0.0f}, or vice
* versa, the {@code equal} test has the value
* {@code false}, even though {@code 0.0f==-0.0f}
* has the value {@code true}.
* </ul>
*
* This definition allows hash tables to operate properly.
* @apiNote
* This method is defined in terms of {@link
* #floatToIntBits(float)} rather than the {@code ==} operator on
* {@code float} values since the {@code ==} operator does
* <em>not</em> define an equivalence relation and to satisfy the
* {@linkplain Object#equals equals contract} an equivalence
* relation must be implemented; see <a
* href="Double.html#equivalenceRelation">this discussion</a> for
* details of floating-point equality and equivalence.
*
* @param obj the object to be compared
* @return {@code true} if the objects are the same;
* {@code false} otherwise.
* @see java.lang.Float#floatToIntBits(float)
* @jls 15.21.1 Numerical Equality Operators == and !=
*/
public boolean equals(Object obj) {
return (obj instanceof Float)
@ -884,24 +880,32 @@ public final class Float extends Number
public static native float intBitsToFloat(int bits);
/**
* Compares two {@code Float} objects numerically. There are
* two ways in which comparisons performed by this method differ
* from those performed by the Java language numerical comparison
* operators ({@code <, <=, ==, >=, >}) when
* applied to primitive {@code float} values:
* Compares two {@code Float} objects numerically.
*
* <ul><li>
* {@code Float.NaN} is considered by this method to
* be equal to itself and greater than all other
* {@code float} values
* (including {@code Float.POSITIVE_INFINITY}).
* <li>
* {@code 0.0f} is considered by this method to be greater
* than {@code -0.0f}.
* This method imposes a total order on {@code Float} objects
* with two differences compared to the incomplete order defined by
* the Java language numerical comparison operators ({@code <, <=,
* ==, >=, >}) on {@code float} values.
*
* <ul><li> A NaN is <em>unordered</em> with respect to other
* values and unequal to itself under the comparison
* operators. This method chooses to define {@code
* Float.NaN} to be equal to itself and greater than all
* other {@code double} values (including {@code
* Float.POSITIVE_INFINITY}).
*
* <li> Positive zero and negative zero compare equal
* numerically, but are distinct and distinguishable values.
* This method chooses to define positive zero ({@code +0.0f}),
* to be greater than negative zero ({@code -0.0f}).
* </ul>
*
* This ensures that the <i>natural ordering</i> of {@code Float}
* objects imposed by this method is <i>consistent with equals</i>.
* objects imposed by this method is <i>consistent with
* equals</i>; see <a href="Double.html#equivalenceRelation">this
* discussion</a> for details of floating-point comparison and
* ordering.
*
*
* @param anotherFloat the {@code Float} to be compared.
* @return the value {@code 0} if {@code anotherFloat} is
@ -912,8 +916,8 @@ public final class Float extends Number
* {@code Float} is numerically greater than
* {@code anotherFloat}.
*
* @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}
* @since 1.2
* @see Comparable#compareTo(Object)
*/
public int compareTo(Float anotherFloat) {
return Float.compare(value, anotherFloat.value);