Support for primitive types in instanceof and type patterns
Joseph D. Darcy
joe.darcy at oracle.com
Tue Nov 15 22:40:02 UTC 2022
On 11/15/2022 12:13 PM, Brian Goetz wrote:
>
>
> On 11/15/2022 2:55 PM, Dan Heidinga wrote:
>> Hi Angelos,
>>
>> The overall direction and writeup for this looks good to me. I think
>> we need to be more specific about how doubles & floats fit into this
>> though as they are perpetual source of special cases.
>
> Indeed, floating point is the gift that keeps on giving.
>
> Joe recently did some nice work in the spec of Float and Double,
> outlining the various different ways in which floats can be compared.
> This allows language features like pattern matching, casting, and
> switch to appeal to "compared by FooBar comparison", rather than
> spelling it out for each use.
>
>
FYI, the new text in question is:
https://download.java.net/java/early_access/jdk20/docs/api/java.base/java/lang/Double.html#fpNumericalEq
For the cases in the draft JEP, there have been separate discussions
working through the details. Putting aside how it might be implemented,
for the
floating-point type -> integral type
cases, conceptually if the same real number can be presented in the
floating-point type and the integral type, then the conversion is exact.
(While floating-point values are often thought of as "fuzzy," each
finite floating-point value represents some particular real number.
Finite binary floating-point values are sums of the powers of 2 where
the exponent difference of the powers is bounded by the significand
width of the floating-point format in question.) The main special case
is how conversion of -0.0 to an integral type is defined for purposes of
instanceof. The desired double -> float behavior can be inferred from
IEEE 754 concepts.
HTH,
-Joe
More information about the amber-spec-observers
mailing list