I have an issue when doing equality on Floats wrap...
# multiplatform
c
I have an issue when doing equality on Floats wrapped in value classes, essentially I have the following:
Copy code
@JvmInline
value class F32(override val value: Float) : NumberValue<Float>
I have some code which compares lists of F32 which other lists of F32 to assert that they match, now when both floats are NAN the equality operation behaves different between JVM and Native platforms. On JVM it works and says they are equal but fails on Native
Copy code
if (listOf(F32(Float.Nan)) == listOf(F32(Float.Nan) {
Could this be something to do with value classes implementation on native?
a
Since
NaN != NaN
, I'd actually expect the lists to be not equal.
☝️ 1
c
NaN is unordered, so a numeric comparison operation involving one or two NaNs returns
false
and any
!=
comparison involving NaN returns
true
, including
x!=x
when
x
is NaN.
https://docs.oracle.com/javase/specs/jls/se6/html/typesValues.html
c
So Kotlin seems to be quite inconsistent after spending a fair amount of time debugging this, so:
Copy code
Float.NaN == Float.NaN // false

listOf(Float.NaN) == listOf(Float.NaN) // true
Which is weird but it gets weirder, there are cases, only on native (jvm is consistent on the above). Where if the NaN is the result of a computation as opposed to the hardcoding it, where
Copy code
listOf(Float.NaN) == listOf(Float.Nan) // false
As as result I’ve had to roll my own equality checks in all places where floats are involved. If any of the devs are interested I can recreate the behaviour consistently
a