gpt4 book ai didi

scala - Scala 中 Long.toInt 的语义是什么?

转载 作者:行者123 更新时间:2023-12-02 06:51:34 25 4
gpt4 key购买 nike

如果是 long.isValidInt,那么很明显,它求值为相应的 Int 值。

但如果不是呢?是否等同于简单地删除前导位?

最佳答案

Is it equivalent to simply dropping the leading bits?

是的。要验证这一点,您可以尝试一下或引用 following section of the Scala specification :

Conversion methods toByte, toShort, toChar, toInt, toLong, toFloat, toDouble which convert the receiver object to the target type, using the rules of Java's numeric type cast operation. The conversion might truncate the numeric value (as when going from Long to Int or from Int to Byte) or it might lose precision (as when going from Double to Float or when converting between Long and Float).

还有 corresponding section of the Java specification :

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

为什么在 ScalaDocs 中不只是描述 toInt 方法,我不知道。

关于scala - Scala 中 Long.toInt 的语义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43017070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com