dtpfia3334 2019-07-12 04:16
浏览 535
已采纳

为什么在这种情况下float32比float64更准确?

If I run the following piece of Go code:

fmt.Println(float32(0.1) + float32(0.2))
fmt.Println(float64(0.1) + float64(0.2))

the output is:

0.3
0.30000000000000004

It appears the result of the float32 sum is more exact than the result of the float64 sum, why? I thought that float64 is always more precise than float32. How do I decide which one to pick to have the most accurate result?

  • 写回答

1条回答 默认 最新

  • doushun1904 2019-07-12 04:42
    关注

    It isn't. fmt.Println is just making it look more precise. Println uses %g for floating point and complex numbers. The docs say...

    The default precision for... %g it is the smallest number of digits necessary to identify the value uniquely.

    0.3 is sufficient to identify a float32. But float64 being much more precise needs more digits.

    We can use fmt.Printf and %0.20g to force both numbers to display the same precision.

    f32 := float32(0.1) + float32(0.2)
    f64 := float64(0.1) + float64(0.2)
    
    fmt.Printf("%0.20g
    ", f32)
    fmt.Printf("%0.20g
    ", f64)
    
    0.30000001192092895508
    0.30000000000000004441
    

    float64 is more precise. Neither are exact as that is the nature of floating point numbers.

    We can use strconv.FormatFloat to see what these numbers really are.

    fmt.Println(strconv.FormatFloat(float64(f32), 'b', -1, 32))
    fmt.Println(strconv.FormatFloat(f64, 'b', -1, 64))
    
    10066330p-25
    5404319552844596p-54
    

    That is 10066330 * 2^-25 and 5404319552844596 * 2^-54.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上