# Tonight I hated julia, REPL and overflows

**URL:** <https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884>\
**Category:** New to Julia\
**Tags:** integer-overflow\
**Created:** [February 20, 2020, 2:10am UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884 "2020-02-20T02:10:21Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [February 20, 2020, 3:03pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/21 "2020-02-20T15:03:28Z")

</div>

It seems, you miss some information: CPU manufacturers have split from CPU designers.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [February 20, 2020, 4:28pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/22 "2020-02-20T16:28:20Z")

</div>

> [@DNF](#):
>
> p(n) = 1 - prod(((365-n+1):365)./365)

or:

```julia
julia> pb(n) = 1 - prod(((365-n+big(1//1)):big(365//1))./365)
pb (generic function with 1 method)

julia> pb(25)
5689337892098486666289143070200558294086544632989664632317//10004116148447957520459906484225353834116619892120361328125

julia> pb(365)
515170326658127653023270268934968831864006656970059803702471762685413738371922676540955719255176033837628306315825181877150741759238731487936775212190033564060565217031650870926726076285585134244114338983629673474603758922530105651496038373092997439767901114109920461090880690975928175098618904132837793365308910108896606269503293041714459828188935384156346073033357594771669030388816625948353617734957752811385856527124319037902117181250872513088032449678050159472935289739052454666983371294578747238788374891057467990003992245887195172160776203739789136944311263270748379507317313573362798868390016430415820186725989354970316276107040564983619367087866763697012915563702134139411371829660607280978660535278554405074569414248808971704250775731531850203674614133069776577836319704570703710916971591744825994565287963159063095105536142176722208706018846440325979761//515170326658127653023270268934968831864006656970059803702471762685413738371922676540955719255176033837628306315825181877150741759238731487936775212190033564135520192401974269458851863217125548343091322801534117245573443413531922275612913528038368117246368938917573264013996463001304954829864348817232466436717311856873167200871359539951780183986982019981454546048785471012647019662520753069879400574056837803950541557117787442934714835950267467275488609709127945889546366868819498143130928828104709275844760637935840192366628163101930811267397402954354789999800088756824182179122318041220995650232069431164178955161360483122690952293589731983424333163050451909518452256437800568549944499327426042068267202215812972708997217666469620959661388470725356466960622014888773469229307515572285821265768570192890040020733139958437375727218210386126884259283542633056640625

julia> pb(366)
1//1

julia> Float64(1-pb(365))
1.4549552156187034e-157

```

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [February 20, 2020, 8:00pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/23 "2020-02-20T20:00:10Z")

</div>

> [@benibilme](#):
>
> Then I tried do the math with BigInt at RELP. Julia says no, I can not create typed variable at global scobe in RELP, (why? I am sure there is very detailed reasoning but, hey this language supposed to replace other math environment, and help people interactive analysis right?)

Global constants are for typed variable creation in global scope. Here, an example

```julia
julia> const a = big"100"
100

julia> a = 10
ERROR: invalid redefinition of constant a
Stacktrace:
 [1] top-level scope at REPL[2]:1

julia> a = big"10"
WARNING: redefining constant a
10

```

And if you input a big `n` value, you’ll get what you want.

```julia
julia> p(n) = 1 - prod(365:-1:(365-n+1)) / 365^n
p (generic function with 1 method)

julia> p(a)
0.1169481777110776518691647704154958387683426672989827140599909452015520092018471

julia> p(big"366")
1.0

julia> p(big"25")
0.5686997039694638856178840908472239012386527193977018154729818882451772362311903

```

It is better to show what you used but got error. Then, you get useful understanding from more knowledgeable person here, after all, this place is for that. Maybe, you mistakenly used older syntax from before 1.0 era or something else. At this, point we only have guesses that can surely be wrong.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [February 21, 2020, 5:00am UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/24 "2020-02-21T05:00:06Z")

</div>

> [@DNF](#):
>
> The global scope typing issue doesn’t have anything to do with being able to create variables

I think the OP may be referring to this

```julia
julia> x::Int = 1
ERROR: syntax: type declarations on global variables are not yet supported
Stacktrace:
 [1] top-level scope at REPL[1]:1

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 21, 2020, 5:11am UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/25 "2020-02-21T05:11:47Z")

</div>

That’s what I assumed and I mentioned it in the first post.

---

<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, 2020, 10:37am UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/26 "2020-02-21T10:37:01Z")

</div>

This approach is not only used in common Lisp, but in all languages mentioned where Ints do not overflow: python, ruby, mathematica, magma, gap … Comparatively, BigInts are _very_ slow in julia  
(at least 50 times slower). It would be really useful if someone implemented such a type in julia (unfortunately, still beyond my skills)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 21, 2020, 10:41am UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/27 "2020-02-21T10:41:08Z")

</div>

Wouldn’t

```julia
struct BigIntOnDemand
    value::Union{Int,BigInt}
end

```

almost do this? Not with packed bits but a union tag, but it is very close.

---

<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, 2020, 1:11pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/28 "2020-02-21T13:11:23Z")

</div>

You would need `Union{Int,Int128,BigInt}` for optimal efficiency. And where the skill is needed is writing the arithmetic operations/overflow detection and handling for such a type so that they are fast.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 21, 2020, 1:27pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/29 "2020-02-21T13:27:17Z")

</div>

It was mentioned before. It’ll be very hard or impossible to get full speed of `Int` from this (mostly because of vectorization). It will be a good implementation of `BigInt` although I thought `GMP` is doing that already. Also, as someone (I believe it was @stevengj) pointed out last time, for most problems where you truely need `BigInt`, most of the time you won’t benefit from this optimization since you spend most time when the number is actually big.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 21, 2020, 5:23pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/30 "2020-02-21T17:23:19Z")

</div>

You probably don’t want that since it makes the storage unnecessarily large. You’d want the value to either be represented as an inline Int or a pointer to a heap-allocated BigInt. And the garbage collector would need to understand which was which. This could potentially be hacked into the system in C code as a special type, but that’s pretty unsatisfying, which is why no one has done it. The “right” way to do this instead would be to enable efficient inline/pointer unions like that as user-defined types and then define a hybrid BigInt type that way. And of course, you’d also want to teach the compiler that it can reuse heap-allocated BigInt values if it’s sure that no one else has a reference—otherwise you’ll have better performance until values get large but then have a sudden drop-off as soon as they get too big. But I’m sure you can see why all of this hasn’t been done yet.

---

<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, 2020, 6:04pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/31 "2020-02-21T18:04:31Z")

</div>

I would argue the opposite: when I need `BigInt` in a mathematical computation, usually only a few of the numbers involved are too big for `Int`. I have many examples which run faster in ruby/gap/magma because I have to run them with `BigInt` in Julia while the other systems integer type are sometimes 50 times faster than `BigInt` (which is still 5 times slower than `Int`, which is why I approve the Julia decision to default to `Int`; in Julia `BigInt` is 250 times slower than `Int`).

---

<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, 2020, 6:07pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/32 "2020-02-21T18:07:09Z")

</div>

Yes I know this is difficult. That’s why I said my skills are yet unsufficient.

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [February 21, 2020, 11:47pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/33 "2020-02-21T23:47:17Z")

</div>

you have to ❤ the Julia community.

user makes somewhat inflammatory post.

Julia community responds with reasonable answers/comments to said post and then proceeds with in-depth discussion of efficient big-num/big-int implementation.

😆

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [February 22, 2020, 7:09am UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/34 "2020-02-22T07:09:03Z")

</div>

…and we lost him 😉

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 22, 2020, 1:04pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/35 "2020-02-22T13:04:22Z")

</div>

> [@Jean\_Michel](#):
>
> when I need `BigInt` in a mathematical computation, usually only a few of the numbers involved are too big for `Int`

What I meant is that if you are running a single piece of code multitple times and that line of code either needs or not need `BigInt`, then that line of code will need `BigInt` way more often than not. This is simply because there are way more `BigInt` than `Int`. If you are running a piece of code only once (not in a loop and not with different inputs), then the performance of it doesn’t matter.

Of course it’s entirely possible that only a small portion of your code needs `BigInt` and you should limit your use of `BigInt` to those few lines of code.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [February 22, 2020, 6:14pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/36 "2020-02-22T18:14:58Z")

</div>

> [@yuyichao](#):
>
> Of course it’s entirely possible that only a small portion of your code needs `BigInt` and you should limit your use of `BigInt` to those few lines of code

Normally, big numbers appears in divisions (by another big number) so an alternative approach would be to contain those divisions inside functions.

---

<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 22, 2020, 8:27pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/37 "2020-02-22T20:27:01Z")

</div>

This is not the way it goes. A typical example I had recently is when compute the size of a set, about 10^9 intermediate computations did fit with in Int64 or Int128 and only about 30 did not fit. I have the choice to redo entirely the computation in `BigInt` (very slow) or (better) to use `SaferIntegers` but then it is quite complicated even with a `try` block to switch to `BigInt` to go over an overflow case and back to `SafeInt` when possible…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 22, 2020, 10:46pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/38 "2020-02-22T22:46:51Z")

</div>

So you are saying you repeated the computation 10^9 times?

---

<div class="post-metadata">

**Author:** ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)\
**Post date:** [February 22, 2020, 11:13pm UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/39 "2020-02-22T23:13:13Z")

</div>

> [@Jean\_Michel](#):
>
> have the choice to redo entirely the computation in `BigInt` (very slow) or (better) to use `SaferIntegers`

Maybe try BitIntegers.jl, they work well for me, much faster than bigints

> [@ANN: BitIntegers.jl (Int256, ...) and BitFloats.jl (Float80, Float128)](https://discourse.julialang.org/t/ann-bitintegers-jl-int256-and-bitfloats-jl-float80-float128/15935):
>
> I’m glad to present two new registered packages which add more “native-like” types to Julia, mostly implemented in the same way as Base builtin integer and floating point types. Both packages can lead to segfaults that I don’t understand, and they are under-tested, so they must be considered as experimental; other contributors will be needed to overcome the shortcomings (read the respective READMEs for more information). That said, they can already be useful smiley[BitIntegers.jl](https://github.com/rfourquet/BitIntegers.jl) exports s…

---

<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 23, 2020, 12:05am UTC](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884/40 "2020-02-23T00:05:55Z")

</div>

No. I said that the computation demanded 10^9 intermediate computations, which were all different, and that about 30 of them overflowed `Int128`. And that this is fairly typical of the kind of computations that I have to do in representation theory of Hecke algebras, one of my topics.

[Previous page](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884.md?page=1)

[Next page](https://discourse.julialang.org/t/tonight-i-hated-julia-repl-and-overflows/34884.md?page=3)
