So I recently started loving the language kotlin. Today, while comparing doubles, I came across the inevitable NaN
.
fun main(args: Array<String>) {
val nan = Double.NaN
println("1: " + (nan == nan))
println("2: " + (nan == (nan as Number)))
println("3: " + ((nan as Number) == nan))
}
N.B: (Double
is a subtype of Number
)
Running the above code yields:
1: false
2: true
3: true
I understand that comparing with NaN
in Java returns false
, so I would expect false
for all expressions.
How can this behavior be explained? What is the rationale behind it?