Golang浮点运算

Sun*_*dar 0 floating-point floating-accuracy go

Go 中的浮点运算是如何工作的

游乐场链接

package main

import "fmt"

func main() {
    j := 1.021
    fmt.Println(j)
    k := j*1000
    fmt.Println(k)
    l := int(k)
    fmt.Println(l)
}

Output:
1.021
1020.9999999999999
1020
Run Code Online (Sandbox Code Playgroud)

我原本以为会1021被打印出来,但我得到了1020

pet*_*rSO 5

Go 和其他算法一样,使用 IEEE-754 二进制浮点运算。浮点运算对于某些十进​​制值来说是不精确的。

参考:

浮点

IEEE 浮点