# Why can I just write BigInt("12345")

**URL:** <https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941>\
**Category:** General Usage\
**Created:** [September 19, 2019, 11:30am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941 "2019-09-19T11:30:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 19, 2019, 11:30am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/1 "2019-09-19T11:30:30Z")

</div>

According to [Mathematicians No Longer Stumped By The Number 3](https://www.gizmodo.com.au/2019/09/mathematicians-no-longer-stumped-by-the-number-3/)

Just on the heels of finding three cubed numbers that [sum to 42], scientists have passed another important milestone by finding three enormous cubes that sum to 3.

```julia
julia> parse(BigInt,"569936821221962380720")^3 + parse(BigInt,"-569936821113563493509")^3 + parse(BigInt,"-472715493453327032")^3
3

```

But why is it so difficult to verify using julia.

Why can’t I just write

```julia
julia> bigint("569936821221962380720")^3 + bigint("-569936821113563493509")^3 + bigint("-472715493453327032")^3
ERROR: UndefVarError: bigint not defined
Stacktrace:
 [1] top-level scope at REPL[11]:1

julia> BigInt("569936821221962380720")^3 + BigInt("-569936821113563493509")^3 + BigInt("-472715493453327032")^3
ERROR: MethodError: no method matching BigInt(::String)
Closest candidates are:
  BigInt() at gmp.jl:56
  BigInt(::BigInt) at gmp.jl:254
  BigInt(::Union{Int32, Int64}) at gmp.jl:285
  ...
Stacktrace:
 [1] top-level scope at REPL[12]:1

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 19, 2019, 11:34am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/2 "2019-09-19T11:34:22Z")

</div>

```julia
julia> big"569936821221962380720"^3 + big"-569936821113563493509"^3 + big"-472715493453327032"^3
3

```

Edit: As said below, just using a number to `big` is in this case probably better since you are not doing any computations on the number before turning it into a `BigInt` and Julia will automatically chose a number type that is big enough for numeric literals.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 19, 2019, 11:35am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/3 "2019-09-19T11:35:06Z")

</div>

> [@StevenSiew](#):
>
> ```julia-auto
> bigint("569936821221962380720")^3 + bigint("-569936821113563493509")^3 + bigint("-472715493453327032")^3
> 
> ```

```julia-auto
julia> big(569936821221962380720)^3 + big(-569936821113563493509)^3 + big(-472715493453327032)^3
3

```

There’s no `bigint` function. Simple as that. Just use `big`.

---

<div class="post-metadata">

**Author:** ![korsbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korsbo/32/15942_2.png) [@korsbo](https://discourse.julialang.org/u/korsbo)\
**Post date:** [September 19, 2019, 11:35am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/4 "2019-09-19T11:35:22Z")

</div>

```julia
julia> BigInt(569936821221962380720)^3 + BigInt(-569936821113563493509)^3 + BigInt(-472715493453327032)^3
3

```

---

<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:** [September 19, 2019, 11:38am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/5 "2019-09-19T11:38:21Z")

</div>

Perhaps `big"..."` could be mentioned in [Integers and Floating-Point Numbers · The Julia Language](https://docs.julialang.org/en/v1.4-dev/manual/integers-and-floating-point-numbers/#Arbitrary-Precision-Arithmetic-1) .

I will make a PR.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 19, 2019, 11:39am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/6 "2019-09-19T11:39:13Z")

</div>

> [@korsbo](#):
>
> julia\> BigInt(569936821221962380720)^3 + BigInt(-569936821113563493509)^3 + BigInt(-472715493453327032)^3 3

How come I dont need to enclose it with a string. Why does it not turn to Int64 before being process by the function BigInt()

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 19, 2019, 11:41am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/7 "2019-09-19T11:41:31Z")

</div>

> [@StevenSiew](#):
>
> How come I dont need to enclose it with a string. Why does it not turn to Int64 before being process by the function

How come I dont need to enclose it with a string. Why does it not turn to Int64 before being process by the function big()

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 19, 2019, 11:42am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/8 "2019-09-19T11:42:15Z")

</div>

Because the input is a number literal. The compiler sees that it’s too big for `Int64`. It’s actually a `Int128` before the `BigInt` construction:

```julia
julia> typeof(569936821221962380720)

Int128

```

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 19, 2019, 11:45am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/9 "2019-09-19T11:45:10Z")

</div>

```julia
julia> typeof(1234567890123456789012345678901234567890)
BigInt

julia> 1234567890123456789012345678901234567890^3
1881676372353657772546716040595286755373973700255343476997709998147026668834432100633207693797722198701224860897069000

```

hmmm seems like I only need to use big(12345) if the number is NOT big enough!

This is counter-intuitive

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [September 19, 2019, 11:52am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/10 "2019-09-19T11:52:25Z")

</div>

Well, by default all integers are `Int` (if it fits). If you want something else you need to use `UInt8(myint)` or `big(myint)` etc.

You need to convert by using `big` if you know the result is going to overflow otherwise:

```julia
julia> typemax(Int)^3
9223372036854775807

julia> big(typemax(Int))^3
784637716923335095224261902710254454442933591094742482943

```

---

<div class="post-metadata">

**Author:** ![kurbkid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kurbkid/32/9791_2.png) [@kurbkid](https://discourse.julialang.org/u/kurbkid)\
**Post date:** [September 19, 2019, 11:55am UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/11 "2019-09-19T11:55:59Z")

</div>

> [@StevenSiew](#):
>
> hmmm seems like I only need to use big(12345) if the number is NOT big enough!  
> This is counter-intuitive

Why? It’s just like that 1.0 is automatically a Float64, while 1 is automatically Int. You only need to specify what you want if it is not automatically obvious.

---

<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:** [September 19, 2019, 12:06pm UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/12 "2019-09-19T12:06:02Z")

</div>

[https://github.com/JuliaLang/julia/pull/33322](https://github.com/JuliaLang/julia/pull/33322)

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [September 19, 2019, 12:15pm UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/13 "2019-09-19T12:15:14Z")

</div>

Would that be a good place to also mention/refer to `big()`?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 19, 2019, 12:15pm UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/14 "2019-09-19T12:15:50Z")

</div>

> [@StevenSiew](#):
>
> This is counter-intuitive

It’s just a kind of literal. `0x234` gets parsed as `UInt16`. Long sequences of digits get parsed as `Int128` or `BigInt`, depending on length.

What do you think the compiler should do with something like `1234567890123456789012345678901234567890`?

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 19, 2019, 1:28pm UTC](https://discourse.julialang.org/t/why-can-i-just-write-bigint-12345/28941/15 "2019-09-19T13:28:17Z")

</div>

> [@StevenSiew](#):
>
> hmmm seems like I only need to use big(12345) if the number is NOT big enough!
> 
> This is counter-intuitive

If you don’t like using `big()`, then you have to implement some kind of overflow checking which is not trivial and most high performance languages don’t do. I only know about `checked` keyword in C#, a similar functionality can be achieved with a package, but forcing this behavior language-wide is a big performance killer and source of more bugs. `big` is not worse than `checked` I guess.
