从代码看到浮点数的数值并不一定等于内存中保存的该数的数值

Why am I losing precision while converting float32 to float64?

While converting a float32 number to float64 precision is being lost in Go. For example converting 359.9 to float64 produces 359.8999938964844. If float32 can be stored precisely why is float64 losing precision?

Sample code:

package main

import (
    "fmt"
)

func main() {
    var a float32 = 359.9
    fmt.Println(a)
    fmt.Println(float64(a))
}

answer:

You never lose precision when converting from a float (i.e. float32) to a double (float64). The former must be a subset of the latter.

It’s more to do with the defaulting precision of the output formatter.

The nearest IEEE754 float to 359.9 is

359.899993896484375
The nearest IEEE754 double to 359.9 is

359.8999999999999772626324556767940521240234375
The nearest IEEE754 double to 359.899993896484375 is

359.899993896484375
(i.e. is the same; due to the subsetting rule I’ve already mentioned).

So you can see that float64(a) is the same as float64(359.899993896484375) which is 359.899993896484375. This explains that output, although your formatter is rounding off the final 2 digits.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值