# Julia messes up integer exponents?

**URL:** <https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773>\
**Category:** Performance\
**Tags:** integer-overflow\
**Created:** [February 14, 2019, 4:19am UTC](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773 "2019-02-14T04:19:42Z")\
**Posts on this page:** 7\
**Page:** 6

<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:** [February 17, 2019, 8:29pm UTC](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773/105 "2019-02-17T20:29:23Z")

</div>

fyi SafeInt32 is type stable, as are all the SaferInteger types

```julia
julia> using SaferIntegers

julia> safe32 = SafeInt32(31); safe32 += 1; safe32, typeof(safe32)
(32, SafeInt32)

```

---

<div class="post-metadata">

**Author:** ![jtravs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtravs/32/2010_2.png) [@jtravs](https://discourse.julialang.org/u/jtravs)\
**Post date:** [February 17, 2019, 11:04pm UTC](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773/106 "2019-02-17T23:04:19Z")

</div>

> [@MrRobot](#):
>
> they will also be aware to just use eN rather than 10^N.

or just `10.0^N`

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [February 18, 2019, 12:51am UTC](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773/107 "2019-02-18T00:51:20Z")

</div>

> [@bennedich](#):
>
> what if you have a “checked arithmetic” flag that only affects code in the user’s package/module

If you mean to check arithmetic operations syntactically present in user’s code, I guess it can be done already with a module exporting checked version of these operations? You can put `using CheckedArithmetics` (say) in the beginning of the module (maybe with `if ENV["CHECK_ARITHMETICS"] == "true" ... end` so that it’s easier to turn on/off). I used the same trick to [change the associativity of `*`](https://discourse.julialang.org/t/why-is-multiplication-a-b-c-left-associative-foldl-not-right-associative-foldr/17552/27).

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [February 18, 2019, 1:29am UTC](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773/108 "2019-02-18T01:29:41Z")

</div>

> [@MrRobot](#):
>
> The hard part is putting the warning somewhere that it will be read. Someone who has programmed in other high level languages is not likely to read the Integer sections in the manual, they will be focused on the parts of Julia that are notably different: multiple dispatch, etc… those are the things that I think get priority from new users to Julia.

That’s probably not an unreasonable comment. Maybe that adding a note about integer overflow to common Julia 101 tutorials would be useful. I’ve just made a [PR](https://github.com/adambard/learnxinyminutes-docs/pull/3478) to learn x in y minutes for this.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [February 18, 2019, 1:36am UTC](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773/109 "2019-02-18T01:36:08Z")

</div>

As a couple tests, first a dead-simple function to vectorize (dot product):

```julia
julia> using SaferIntegers, BenchmarkTools

julia> function my_dot(x, y)
           out = zero(promote_type(eltype(x), eltype(y)))
           @inbounds @simd ivdep for i in eachindex(x, y)
               out += x[i] * y[i]
           end
           out
       end
my_dot (generic function with 1 method)

julia> function my_dot_safeint(x, y)
           out = zero(promote_type(eltype(x), eltype(y)))
           N = SafeInt64(min(length(x),length(y)))
           @inbounds @simd ivdep for i in 1:N
               out += x[i] * y[i]
           end
           out
       end
my_dot_safeint (generic function with 1 method)

julia> x = rand(Float32, 512);

julia> y = rand(Float32, 512);

julia> @btime $x' * $y
  24.058 ns (0 allocations: 0 bytes)
125.47223f0

julia> @btime my_dot($x, $y)
  13.664 ns (0 allocations: 0 bytes)
125.47223f0

julia> @btime my_dot_safeint($x, $y)
  14.930 ns (0 allocations: 0 bytes)
125.47223f0

```

And the compiler can still vectorize.  
Make things just a little more complicated:

```julia
julia> using SLEEF #I am currently on the SIMDPirates branch

julia> function my_exp!(x, y)
           @inbounds for i in eachindex(x, y)
               x[i] = SLEEF.exp(y[i])
           end
       end
my_exp! (generic function with 1 method)

julia> function my_exp_safeint!(x, y)
           N = SafeInt64(min(length(x),length(y)))
           @inbounds for i in 1:N
               x[i] = SLEEF.exp(y[i])
           end
       end
my_exp_safeint! (generic function with 1 method)

julia> @btime my_exp!($x, $y)
  146.227 ns (0 allocations: 0 bytes)

julia> all(x .≈ exp.(y))
true

julia> @btime my_exp_safeint!($x, $y)
  1.953 μs (0 allocations: 0 bytes)

julia> all(x .≈ exp.(y))
true

julia> 1.953 / 146.227 * 1e3
13.355946576213695

```

and it does not, and we see an \> 13-fold regression.  
The computer I tested on has avx512, which means 16 Float32 per SIMD unit.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 20, 2019, 10:18am UTC](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773/110 "2019-02-20T10:18:56Z")

</div>

Did you file an issue? Please link it!

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [February 21, 2019, 11:59pm UTC](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773/111 "2019-02-21T23:59:02Z")

</div>

[https://github.com/JuliaLang/julia/issues/31143](https://github.com/JuliaLang/julia/issues/31143)

[Previous page](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773.md?page=5)
