Closed helins closed 3 years ago
Interesting and slightly scary that the JVM casts NaN to 0!
inc
currently performs an implicit cast to long
(behaves like (+ 1 (long arg))
. Perhaps we should change this to retain the type of its argument, so that is behaves like (+ 1 arg)
which is probably more intuitive.
(Actually, my bad, the JVM cast Infinity
and -Infinity
to Long.MAX_VALUE
and Long.MIN_VALUE
respectively, but still the same logic)
Yes, and I believe it is a floating math rule that any computation involving NaN
results in NaN
.
Fixed in a recent-ish commit.
Unlike in other languages, those two functions cast NaN and +-Infinity to 0 producing this kind of inconsistent behavior:
This is due to the fact that in Java, casting those to
long
indeed produces 0. Indeed:However, B is the right way to handle it. Other math functions seem to behave well because they don't cast doubles to longs (they do the opposite if needed), meaning that those special values are left intact and follow proper float arithmetics.