# 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:** 1\
**Showing post:** 80

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 15, 2019, 12:08pm UTC](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773/80 "2019-02-15T12:08:35Z")

</div>

One idea would be to enable checked arithmetic during unit testing, or have some global flag in your project indicating whether you’re running in production mode or not, and use checked arithmetic if not.

I’m sure (and I really hope) there are better ways of doing this, but here’s a simple proof-of-concept:

```julia
function enable_checked_exp()
    @eval Base begin
        function ^(a::T, b::T) where {T<:BitInteger}
            r1 = invoke(^, Tuple{Integer, Integer}, a, b)
            r2 = BigInt(a) ^ BigInt(b)
            r1 == r2 ? r1 : throw(OverflowError("$a^$b: got $r1, expected $r2"))
        end
    end
end

function gracefully_disable_checked_exp()
    atexit(() -> run(`julia -q`))
    exit()
end

```

Demonstration:

```julia
julia> 6.023 * 10^23
1.2068671807961137e18

julia> enable_checked_exp();

julia> 6.023 * 10^23
ERROR: OverflowError: 10^23: got 200376420520689664, expected 100000000000000000000000

julia> 10^17
100000000000000000

julia> gracefully_disable_checked_exp()

julia> 6.023 * 10^23
1.2068671807961137e18

```

---

_[View the full topic](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773)._
