Golang浮点运算[重复]
问题描述:
This question already has an answer here:
- Is floating point math broken? 30 answers
How does the float arithmetic work in 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
I was expecting 1021
to be printed, but I got 1020
</div>
此问题已经存在 在这里有答案: p>
-
浮点数学运算符被破坏了吗?
30个答案
span>
ul>
div>
浮点运算在Go中如何工作 p>
游乐场链接 p>
包main import“ fmt” \ n func main(){ j:= 1.021 fmt.Println(j) k:= j * 1000 fmt.Println(k) l:= int(k) fmt.Println(l ) } 输出: 1.021 1020.9999999999999 1020 code> pre>
我期望打印
1021 code> ,但我得到
1020 code> p> div>
答
Go, like everyone else, uses IEEE-754 binary floating-point arithmetic. Floating-point arithmetic is inexact for some decimal values.
References: