Golang 浮点运算

标签 go floating-point floating-accuracy

<分区>

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

Playground link

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

我期待打印 1021,但我得到了 1020

最佳答案

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

引用资料:

Floating point

IEEE floating point

关于Golang 浮点运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33640943/

相关文章:

golang-migrate Close() 不关闭连接

json - GoLang 结构到 JSON 不转换

performance - 32位和64位浮点性能

java - 如何从 Scanner 中获取多个浮点并将它们单独存储在 Java 中?

math - float 学有问题吗?

go - operator-sdk:出现错误-击中了不受支持的类型无效类型为无效类型

database - 如何在 bolt 中获取桶中的键数

python - 使用 F-string 显示 float

java - 计算 float 时可能出现的最大舍入误差

c++ - 将 `std::floor()` 和 `std::ceil()` 转换为整数类型是否总是给出正确的结果?