Charlie Tapping
05/12/2024, 1:00 PM@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
if (listOf(F32(Float.Nan)) == listOf(F32(Float.Nan) {
Could this be something to do with value classes implementation on native?Arkadii Ivanov
05/12/2024, 4:49 PMNaN != NaN
, I'd actually expect the lists to be not equal.chr
05/13/2024, 12:26 AMNaN is unordered, so a numeric comparison operation involving one or two NaNs returnshttps://docs.oracle.com/javase/specs/jls/se6/html/typesValues.htmland anyfalse
comparison involving NaN returns!=
, includingtrue
whenx!=x
is NaN.x
Charlie Tapping
05/13/2024, 6:40 PMFloat.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
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 consistentlyArkadii Ivanov
05/13/2024, 9:41 PM