In what situations may `Number.toDouble()` "invol...
# stdlib
h
In what situations may
Number.toDouble()
"involve rounding" as suggested by its API docs?
e
odd integers above 2^53 cannot be exactly represented as doubles
Copy code
>>> (2L shl 53) + 1
res0: kotlin.Long = 18014398509481985
>>> ((2L shl 53) + 1).toDouble().toLong()
res1: kotlin.Long = 18014398509481984
l
Would be great to have this documented in Kotlin stdlib KDoc
e
does it have to be enumerated? javadoc uses the same phrasing
clearly the range of various numbers can't all fit in Double - there are other Number types such as BigInteger and BigDecimal as well
h
Thanks for the great answers. I agree, it does not seem to be "rounding"" if a
Number
value does not fit into the target type range, so some more details in the
Number
API docs would be great.
To be fair, most methods the
Number
docs state that it
may involve rounding or truncation.
which covers range issues. So the docs are somehow correct, although the big odd integers to double example is neither rounding nor truncation, so there is imho some inprecision in the docs.
e
why wouldn't it be rounding? it rounds to a representable value
it just can't be called truncation, because when it runs out of finite values it goes infinite:
Copy code
2.toBigInteger().pow(1024).toDouble() == Double.POSITIVE_INFINITY
h
That's a clver handing when exceeding the max value. And you're right, its rounding in technical sense that just takes some time to wrap ones head around it.