Feature suggestion: Add static equals methods to Float and Double

Brian Goetz brian.goetz at oracle.com
Sun Jan 6 23:57:02 UTC 2019

Followers of Project Valhalla will see that this issue comes up when 
defining equality on value types.  The relation you are looking for is 
being called "substitutible"; it asks whether there is any way to 
distinguish between two values.  For primitives other than float/double, 
this coincides with `==`, and similarly for references.

An `isSubstitutible()` API point will likely emerge from Valhalla.

On 1/6/2019 2:01 PM, some-java-user-99206970363698485155 at vodafonemail.de 
> My main goal was to provide a way where NaN is equal to NaN and I went with the behavior of the
> respective wrapper classes, Float and Double, which consider -0.0 and +0.0 as not equal.
> The static method could be called "exactlyEquals" if that is better.
> It might also depend on the usecase whether you want -0.0 being equal to +0.0. Maybe an additional
> method would be required for the case you are describing, e.g. "valueEquals" (though that can be
> ambiguous).
>> Hans Boehm <hboehm at google.com> hat am 6. Januar 2019 um 19:40 geschrieben:
>> What's the motivation for making it easier to spell a comparison method that tells me that -1.0*0.0 is not equal to 1.0*0.0? I would have thought that making this somewhat hard to write is a feature?
>> I agree that there are cases in which this method is useful. But they all seem esoteric enough to me that it's not unreasonable for users to have enough expertise to write the method. Or at least the method should have a name that makes the unusual behavior explicit.

More information about the core-libs-dev mailing list