我知道有一个简单的答案,但它让我感到困惑.
为什么Long.MaxValue可以存储在Double中并以longValue形式返回,但较小的Long会丢失精度.
我原本预计在53位之后会失去精度?这里发生了什么
scala> val x:Double = Long.MaxValue // x: Double = 9.223372036854776E18 (9223372036854775807)
scala> x == Long.MaxValue // Boolean = true
scala> x.longValue == Long.MaxValue // Boolean = true
scala> val y = 1234567890123456789L // y: Long = 1234567890123456789 // less than Long.MaxValue
scala> y < x // Boolean = true
scala> val z:Double = y // z: Double = 1.23456789012345677E18
scala> z == y // Boolean = true
scala> z.longValue == y // Boolean = false // why …
Run Code Online (Sandbox Code Playgroud) scala ×1