# Division of Powers of Ten is wrong. Why?

**URL:** <https://discourse.julialang.org/t/division-of-powers-of-ten-is-wrong-why/25744>\
**Category:** New to Julia\
**Tags:** numbers\
**Created:** [June 26, 2019, 8:32pm UTC](https://discourse.julialang.org/t/division-of-powers-of-ten-is-wrong-why/25744 "2019-06-26T20:32:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![freemint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freemint/32/20013_2.png) [@freemint](https://discourse.julialang.org/u/freemint)\
**Post date:** [June 26, 2019, 8:32pm UTC](https://discourse.julialang.org/t/division-of-powers-of-ten-is-wrong-why/25744/1 "2019-06-26T20:32:14Z")

</div>

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

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

```

The fix was:

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

```

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

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 26, 2019, 8:34pm UTC](https://discourse.julialang.org/t/division-of-powers-of-ten-is-wrong-why/25744/2 "2019-06-26T20:34:13Z")

</div>

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

```julia
julia> 10^22
1864712049423024128

```

This is because Julia uses machine integer arithmetic — see:

> [@Julia messes up integer exponents?](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773/2):
>
> Julia uses actual machine integer arithmetic, which means that numbers can and do under- and overflow if you try to perform a computation outside of the range of values that can be represented by such an integer. See [Integer overflow - Wikipedia](https://en.wikipedia.org/wiki/Integer_overflow) for more general explanation. If you actually need to deal with integers outside the range of a 64-bit Int, then you might want to use Julia’s built-in support for BigInt (which is much closer to the way integers behave in Python): julia\> big(10)^19 10…

---

<div class="post-metadata">

**Author:** ![freemint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freemint/32/20013_2.png) [@freemint](https://discourse.julialang.org/u/freemint)\
**Post date:** [June 26, 2019, 9:12pm UTC](https://discourse.julialang.org/t/division-of-powers-of-ten-is-wrong-why/25744/3 "2019-06-26T21:12:05Z")

</div>

Thank you,  
is there a way to make Julia warn me about this?  
Edit: Yes there is. It is called safe integers.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [June 26, 2019, 9:45pm UTC](https://discourse.julialang.org/t/division-of-powers-of-ten-is-wrong-why/25744/4 "2019-06-26T21:45:33Z")

</div>

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
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.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 28, 2019, 12:27am UTC](https://discourse.julialang.org/t/division-of-powers-of-ten-is-wrong-why/25744/5 "2019-06-28T00:27:01Z")

</div>

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