https://kotlinlang.org logo
#announcements
Title
# announcements
n

Nicole

06/27/2019, 5:03 PM
Someone did several comparisons with
Float.NaN
which led to this odd result:
Float.NaN === Float.NaN
is
false
with JVM and JS targets, and
true
with Kotlin/Native. — I see that identity equality for
Float
has been deprecated, but still figured I should check if this is a bug (and should be reported) or if it’s just “undefined behavior”
k

karelpeeters

06/27/2019, 5:08 PM
It has been deprecated? Where? How?
n

Nicole

06/27/2019, 5:11 PM
It shows up as deprecated in Intellij:
k

karelpeeters

06/27/2019, 5:14 PM
Ah I didn't see it was
===
, which indeed doesn't really make sense for primitives in the first place.
n

Nicole

06/27/2019, 6:03 PM
It seems like (at least for the JVM, and I suspect the same for JS?) is that the values are being boxed, and since there isn’t a cache of boxed values for floating point values the
===
operator returns false
In native code, I suspect it just doesn’t (or cannot?) box, so it compares the values and they’re the same.
k

karelpeeters

06/27/2019, 6:05 PM
Err no, them not being boxed would return
false
since
Nan != Nan
according to IEEE.
n

Nicole

06/27/2019, 6:08 PM
On the JVM,
(Float.valueOf(10) != Float.valueOf(10))
I figured that explained it, but now I’m just very confused why
Float.NaN === Float.NaN
in native =/
k

karelpeeters

06/27/2019, 6:10 PM
Wrong again,
java.lang.Float.equals
checks for actual bit equality, not IEEE equality. (which it has to because of reflexivity)
so
Float.valueOf(10) == Float.valueOf(10)
on the JVM.
So it's probably the other way around, not boxed on the JVM so unequal but bowed for native so equal.
n

Nicole

06/27/2019, 6:12 PM
I actually ran
(Float.valueOf(10) != Float.valueOf(10))
on a JVM, and it prints true
Copy code
System.out.println("Floats: " + (Float.valueOf(10) != Float.valueOf(10)));
Output:
Floats: true
d

damian

06/27/2019, 6:13 PM
If this is java,
!=
is comparing reference equality of boxes, so that makes perfect sense
k

karelpeeters

06/27/2019, 6:14 PM
Ah but that's Java's
==
so Kotlins
===
, I was talking about Koltin's
==
. You should have said
Float.valueOf(10) !== Float.valueOf(10)
😛
n

Nicole

06/27/2019, 6:16 PM
Basically, I’m just trying to understand how
Float.NaN === Float.NaN
is false on a JVM target, but true in a native context.
This led me to looking at the JVM bytecode generated (which seemd to be Float.NaN == Float.NaN after resolving the
FloatCompanionObject.INSTANCE.getNaN()
references
Just for completeness, I filed a bug about this.
k

karelpeeters

06/27/2019, 7:30 PM
Can I ask why you care about what
===
does? It's deprecated because it was useless and confusing.
20 Views