我试图在我的代码中评估以下示例,但得到错误的答案:
julia> -1.259237254330301e-29*10^29
-9.930838679817422e-11
Run Code Online (Sandbox Code Playgroud)
答案显然是错误的,预计-1.259237254330301.我得到了正确的答案
julia> -1.259237254330301e-29*1e29
-1.2592372543303008
Run Code Online (Sandbox Code Playgroud)
有谁知道原因?
小智 8
尽管Floats有舍入错误,但错误结果的原因来自于在类型中使用带有溢出的整数算术:在64位OS 10 ^ 29上溢出(在JULIA中没有错误).
64-Bit OS: 10^18 = = 1000000000000000000 (64-Bit Integers can hold up to ~ 9,22x10^18)
while: 10^19 turns to: -8446744073709551616 which is wrong, due to overflowing.
Run Code Online (Sandbox Code Playgroud)
如果您选择保持该值的类型capabale结果是正确的:例如:
julia> -1.259237254330301e-29*Int128(10)^29
-1.2592372543303008
Run Code Online (Sandbox Code Playgroud)
要么:
julia> -1.259237254330301e-29*big(10)^29
-1.259237254330300936796903330938514872571882315563023692882767533667837970629477
Run Code Online (Sandbox Code Playgroud)