Someone did several comparisons with `Float.NaN` w...
# announcements
n
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
It has been deprecated? Where? How?
n
It shows up as deprecated in Intellij:
k
Ah I didn't see it was
===
, which indeed doesn't really make sense for primitives in the first place.
n
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
Err no, them not being boxed would return
false
since
Nan != Nan
according to IEEE.
n
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
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
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
If this is java,
!=
is comparing reference equality of boxes, so that makes perfect sense
k
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
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
Can I ask why you care about what
===
does? It's deprecated because it was useless and confusing.