当我将Math.nan()和Math.maxfloat64施放到INT上时,为什么结果在GO1.14.2和GO1.17.2之间有所不同?
package main
import (
"fmt"
"math"
)
func main() {
x, y := math.NaN(), math.MaxFloat64
fmt.Printf("%d\n", int(x))
fmt.Printf("%d\n", int(y))
}
那是我的测试代码段。当我运行上述代码使用GO1.14.2时,结果只是
-9223372036854775808
-9223372036854775808
在GO1.17.2中运行的相同代码,结果是
0
9223372036854775807
我搜索了一个模拟问题:为什么golang的uint64和maxfloat64在golang中等于?,它在不同的硬件环境中说,在不同的硬件环境中,nan.nan.nan.nan ()也许有所不同,但是我在MacOS M1系统中运行了代码,只有Golang版本不同。为什么结果在GO1.14.2和GO1.17.2之间有所不同?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
态
您转换浮点数值
nan
int
。int
类型的有效值不包括nan
值,因此您转换的值不能由类型int
的值表示因此,结果是依赖于实现。基本上,规格允许其从版本更改为平台到平台。您不能(不应该)假设转换结果的特定int
值。Spec: Conversions:
You convert the floating-point value
NaN
toint
. The valid values of theint
type does not include theNaN
value, so the value you convert cannot be represented by a value of typeint
, so the result is implementation-dependent. Basically, the spec allows it to be changed from version to version, from platform to platform. You cannot (should not) assume a specificint
value for the conversion result.