# Multiplication issue

**URL:** <https://discourse.julialang.org/t/multiplication-issue/98986>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 17, 2023, 8:31am UTC](https://discourse.julialang.org/t/multiplication-issue/98986 "2023-05-17T08:31:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 17, 2023, 8:31am UTC](https://discourse.julialang.org/t/multiplication-issue/98986/1 "2023-05-17T08:31:45Z")

</div>

This is really strange that i’m getting a negative answer for this:

```julia
10^19 * 10^-18

```

Output: -8.446744073709551

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 17, 2023, 8:34am UTC](https://discourse.julialang.org/t/multiplication-issue/98986/2 "2023-05-17T08:34:03Z")

</div>

I think it’s the third time in a week that I’m wheeling out this classic thread:

> [@Discussion about integer overflow](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627):
>
> Yes, it is a limitation but I would not call it big since we could use BigInt if Int64 is not enough and an overflow is something we can catch. I find more worrying this: \> julia\> 1 / 10^20000 \> Inf I’d rather have an overflow here than Inf, or at least an Infinitesimal value… Maybe something we could ask for future versions of Julia.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 17, 2023, 8:34am UTC](https://discourse.julialang.org/t/multiplication-issue/98986/3 "2023-05-17T08:34:59Z")

</div>

Crap, you beat me to it. But on second thought, I think the documentation about overflow ([Integers and Floating-Point Numbers · The Julia Language](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Overflow-behavior)) might be more informative and less scary than a whole thread discussing whether or not it is justified?

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 17, 2023, 8:39am UTC](https://discourse.julialang.org/t/multiplication-issue/98986/4 "2023-05-17T08:39:28Z")

</div>

Thank you for your help . 🙂

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 17, 2023, 12:24pm UTC](https://discourse.julialang.org/t/multiplication-issue/98986/5 "2023-05-17T12:24:28Z")

</div>

What’s the performance hit of SaferIntegers.jl these days? Based on simple tests on my machine it seems negligible.

I hope one day we can make that the default, and let people use `@fastmath` or similar to allow silent overflows.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 17, 2023, 12:25pm UTC](https://discourse.julialang.org/t/multiplication-issue/98986/6 "2023-05-17T12:25:42Z")

</div>

For this remark the link posted by @nilshg is actually very appropriate 😉 TLDR: it’s unlikely to change (EDIT: at least anytime soon)

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [May 17, 2023, 12:41pm UTC](https://discourse.julialang.org/t/multiplication-issue/98986/7 "2023-05-17T12:41:50Z")

</div>

```julia
import BenchmarkTools: @btime
import SaferIntegers: SafeInt64

n = 100_000_000

x_unsafe = fill(Int64(1), n)
x_safe = fill(SafeInt64(1), n)

@btime sum($x_unsafe)
@btime sum($x_safe)

```

```julia
julia> @btime sum($x_unsafe)
  12.807 ms (0 allocations: 0 bytes)
100000000

julia> @btime sum($x_safe)
  34.277 ms (0 allocations: 0 bytes)
100000000

```

The penalty seems like 3x perf regression.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 17, 2023, 12:45pm UTC](https://discourse.julialang.org/t/multiplication-issue/98986/8 "2023-05-17T12:45:01Z")

</div>

Not sure about this TLDR: the discussion ends on a note that `Int^Int` will probably become the default (while a general solution would be more difficult to implement).  
But it’s true the discussion in the related [PR](https://github.com/JuliaLang/julia/pull/21600) makes that seem unlikely.

Also the [FAQ](https://docs.julialang.org/en/v1/manual/faq/) says

> checked arithmetic everywhere […] ends up having a substantial cost due to compilers (LLVM and GCC) not gracefully optimizing around the added overflow checks. If this improves in the future, we could consider defaulting to checked integer arithmetic in Julia, but for now, we have to live with the possibility of overflow.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 17, 2023, 12:50pm UTC](https://discourse.julialang.org/t/multiplication-issue/98986/9 "2023-05-17T12:50:10Z")

</div>

> [@johnmyleswhite](#):
>
> The penalty seems like 3x perf regression.

We’ve discussed this in the past ([julia#21600](https://github.com/JuliaLang/julia/pull/21600)), and it was argued that (1) `Int` exponentiation is rarely performance-sensitive, and (2) there may be faster ways to check for overflow (especially for the literal powers that you commonly see in code like this).

The main obstacle is that changing this would be [“too breaking for 1.x”](https://github.com/JuliaLang/julia/pull/21600#issuecomment-1293975250).

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 17, 2023, 12:53pm UTC](https://discourse.julialang.org/t/multiplication-issue/98986/10 "2023-05-17T12:53:56Z")

</div>

Ouch that’s a huge hit! I was testing with the first example on SaferIntegers.jl’s [homepage](https://github.com/JeffreySarnoff/SaferIntegers.jl#benchmarking-on-one-machine) and got no significant difference:

```julia
julia> hundredths( (@belapsed test(n, +, $sa, $sb, $sc, $sd)) /
                   (@belapsed test(n, +, $a, $b, $c, $d)) )
1.0

```
