3

I am trying to evaluate the following example inside my code, but getting the wrong answer:

julia> -1.259237254330301e-29*10^29 
-9.930838679817422e-11

The answer is clearly wrong, -1.259237254330301 is expected. I got the right answer by

julia> -1.259237254330301e-29*1e29
-1.2592372543303008

anyone know the reason?

1 Answers1

8

Despite the rounding errors of Floats, the cause for the wrong result comes from the use of Integer Arithmetic with overflow in the Type: On a 64-Bit OS 10^29 overflows (in JULIA without an error).

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.

If you choose a type capabale of holding the value the result is correct: eg.:

julia> -1.259237254330301e-29*Int128(10)^29
-1.2592372543303008

or:

julia> -1.259237254330301e-29*big(10)^29
-1.259237254330300936796903330938514872571882315563023692882767533667837970629477
Ultima Ratio
  • 141
  • 1
  • 1
  • 3