当我打印出uint64,float32和float64的最大值时,似乎浮动应该容纳uint64。但是,我尝试过多次转换,大型uint64号码无法准确转换。我做错了什么吗?或者我误解了文档和语言限制?
fmt.Printf("Max unit64 value = %d\n", uint64(math.MaxUint64))
fmt.Printf("Max float32 value = %f\n", float32(math.MaxFloat32))
fmt.Printf("Max float64 value = %f\n", float64(math.MaxFloat64))
var ui64 uint64
ui64 = uint64(math.MaxUint64)
fmt.Printf("uint64(math.MaxUint64) = %d\n", ui64)
var f32 float32
f32 = float32(ui64)
fmt.Printf("%d as 32-bit float = %0.f\n", ui64, f32)
var f64 float64
f64 = float64(ui64)
fmt.Printf("%d as 64-bit float = %0.f\n", ui64, f64)
输出:
Max unit64 value = 18446744073709551615
Max float32 value = 340282346638528859811704183484516925440.000000
Max float64 value = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
uint64(math.MaxUint64) = 18446744073709551615
18446744073709551615 as 32-bit float = 18446744073709551616
18446744073709551615 as 64-bit float = 18446744073709551616
去游乐场 - > https://play.golang.org/p/DhlaxbFixBD
我也尝试使用big.Float,它输出正确的Text,但在调用Float64()函数得到一个float64时给出相同的结果。这使得它似乎发生了某种类型的溢出,但我不确定它是否是语言的约束或我做错了什么。非常感谢协助。