Thread
#stdlib
    h

    holgerbrandl

    1 year ago
    In what situations may
    Number.toDouble()
    "involve rounding" as suggested by its API docs?
    e

    ephemient

    1 year ago
    odd integers above 2^53 cannot be exactly represented as doubles
    >>> (2L shl 53) + 1
    res0: kotlin.Long = 18014398509481985
    >>> ((2L shl 53) + 1).toDouble().toLong()
    res1: kotlin.Long = 18014398509481984
    louiscad

    louiscad

    1 year ago
    Would be great to have this documented in Kotlin stdlib KDoc
    e

    ephemient

    1 year ago
    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

    1 year ago
    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

    1 year ago
    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:
    2.toBigInteger().pow(1024).toDouble() == Double.POSITIVE_INFINITY
    h

    holgerbrandl

    1 year ago
    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.