I’ve encountered an error that caused me a lot of grief, can somebody explain why:

```
julia> (10^18)/(10^22)
0.5362758289192254
```

The fix was:

```
julia> (10.0^18)/(10.0^22)
0.0001
```

but i do not see how it ended up with such a wild solution.

I’ve encountered an error that caused me a lot of grief, can somebody explain why:

```
julia> (10^18)/(10^22)
0.5362758289192254
```

The fix was:

```
julia> (10.0^18)/(10.0^22)
0.0001
```

but i do not see how it ended up with such a wild solution.

It’s not the division that was “wrong,” it was the exponentiation.

```
julia> 10^22
1864712049423024128
```

This is because Julia uses machine integer arithmetic — see:

5 Likes

Thank you,

is there a way to make Julia warn me about this?

Edit: Yes there is. It is called safe integers.

1 Like

As someone else has explained, `10^18/10^22`

produces integer `10^18`

divided by integer `10^22`

. Because `10^18`

can be represented correctly by 64 bit integers while `10^19`

and larger numbers are too big for 64 bit integers (and hence cause overflow), number `10^22`

overflows and becomes ca. 1.86\cdot 10^{18}.

If `10^N`

or 10^N is meant to be a real number as in scientific notation, the “correct” syntax in Julia, MATLAB, Python, etc. is `keN`

= k\cdot 10^N (i.e., if you want to use Float64). And then you’ll get the result you probably expect:

```
julia> 1e18/1e22
0.0001
julia> typeof(ans)
Float64
```

If you want Float 32, the syntax for k\cdot 10^N is `kfN`

.

Personally, I don’t think this is a problem at all — as long as one realizes that scientific notation is supposed to be written `keN`

etc. — if anything, it is more systematic than in MATLAB and other languages.

3 Likes

let me know if you have any questions regarding https://github.com/JeffreySarnoff/SaferIntegers.jl

1 Like