float64(0) 和 0.0 参与计算结果有时不同

xuchunyang · · 2386 次点击
`0.0`和`float64(0)`类型不一样,前者是无类型浮点数(精度更高),后者是float64,不同类型在精度上的差别影响了后续的计算过程得到的中间值的精度 > Constant expressions are always evaluated exactly; intermediate values and the constants themselves may require precision significantly larger than supported by any predeclared type in the language. https://golang.org/ref/spec#Constant_expressions
#6
更多评论
这个比较神奇, 不过浮点数本来也不能用==进行比较. 我纳闷的是为啥第一个能相等...
#1
拿代码测试了一下, 其实刚才隐约感觉这个和存储的字节数有关系. ```go a := (float64(0) +1.1) * 5 / 9 + 200.14 b := (0.0 +1.1) * 5 / 9 + 200.14 fmt.Println(a == b, a, b) ``` 不超过255, 就会相等,不知道深层次的原理是什么也许需要调汇编看一下才明白. 不研究了,知道坑避开就行, 浮点数不可以用==进行相等判断是一个rule
#2