[英]Float64 typed constant in go, loses precision
Consider the following code written in go: 考虑一下用go编写的以下代码:
package main
import "fmt"
func main() {
const pi float64 = 22 / 7
fmt.Println("pi value", pi)
}
here the value of pi
is 3
. pi
值为3
。 The question is why is the value losing precision even though it is of type float64
? 问题是,即使值是
float64
类型,为什么值会丢失精度?
NeverMind, I got the answer. NeverMind,我得到了答案。
package main
import "fmt"
func main() {
const pi float64 = 22.0 / 7.0
fmt.Println("pi value", pi)
}
Output 3.142857142857143
. 输出
3.142857142857143
。 it seems types depend if type integer are be divided, it will return a integer regardless if the receiving var is type float64. 似乎类型取决于是否对整数类型进行了除法,无论接收的var是否为float64类型,它都会返回一个整数。 so get a float value, devision has you occur between float values.
因此获得一个浮点值,在两个浮点值之间进行划分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.