https://kotlinlang.org logo
#stdlib
Title
# stdlib
h

holgerbrandl

11/29/2020, 6:40 PM
In what situations may
Number.toDouble()
"involve rounding" as suggested by its API docs?
e

ephemient

11/29/2020, 6:45 PM
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

louiscad

11/29/2020, 6:57 PM
Would be great to have this documented in Kotlin stdlib KDoc
e

ephemient

11/29/2020, 7:04 PM
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

holgerbrandl

11/29/2020, 7:20 PM
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

ephemient

11/29/2020, 7:36 PM
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

holgerbrandl

11/29/2020, 7:42 PM
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.
5 Views