Just my guess: - when use IEE754, 18446744073709551615 will be actual stored as 18446744073709551616 (> 2<<64) - so uint64(float64(18446744073709551615)) is overflow and undefined
Code: // true fmt.Println(uint64(float64(18446742974197923840)) == uint64(18446742974197923840)) // cannot convert float64(18446744073709551615) (constant 18446744073709551616 of type float64) to type uint64 fmt.Println(uint64(float64(18446744073709551615))) 在2023年10月25日星期三 UTC+8 19:25:40<Xie Yuchen> 写道: > Hi Guys, recently, I've been learning the number types conversion. I > learned about the ieee 754 round to even when golang converts from uint64 > to float64. > > However, when I want to convert the float64 to uint64 back, there is a > different between functions: `math.Float64Bits` and use `uint64(f)`. > > Here is the code example: https://go.dev/play/p/Au0YCVKYas8 > > The output is: > ``` > 4895412794951729152 > 1111111111111111111111111111111111111111111111111111111111111111 > 1.8446744073709552e+19 > 4895412794951729152 > 4895412794951729152 > 9223372036854775808 > ``` > The 4895412794951729152 is output by math.Float64Bits and the value > 9223372036854775808 is directly begot from `uint64(f)`. > > I am still confusing where the 9223372036854775808 comes from. Could you > kindly help me if you have some ideas? > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e57baa63-4274-4989-aaa4-4fc9d44d8a27n%40googlegroups.com.