# Representing very big numbers

**URL:** <https://discourse.julialang.org/t/representing-very-big-numbers/84589>\
**Category:** General Usage\
**Tags:** bigfloat\
**Created:** [July 21, 2022, 1:23pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589 "2022-07-21T13:23:44Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [July 21, 2022, 1:23pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/1 "2022-07-21T13:23:44Z")

</div>

How can I perform arithmetic on numbers on the order of a [googloplex](https://en.wikipedia.org/wiki/Googolplex) i.e. `10^(10^100)`?

I don’t have enough memory to express them exactly as integers with all their digits, but I’d like to be able to represent them as `BigFloat`s. The largest I’ve been able to get is approximately `10^(10^18)`:

```julia
n = round(Int, log10(2) * typemax(Int) / 2) - 14
parse(BigFloat, "5.87565378911158759093691e+$n")

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 21, 2022, 1:30pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/2 "2022-07-21T13:30:13Z")

</div>

yeah. `BigFloat` maxes out at `2^(2^62)`. If you want bigger numbers than that, you would need a different number type.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [July 21, 2022, 1:32pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/3 "2022-07-21T13:32:01Z")

</div>

Do you know of any packages that provide types with higher maxima?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 21, 2022, 1:33pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/4 "2022-07-21T13:33:45Z")

</div>

`ArbFloat` goes up to 2^(2^126) (about 10^10^37).

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [July 21, 2022, 1:42pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/5 "2022-07-21T13:42:06Z")

</div>

> [@Oscar\_Smith](#):
>
> `BigFloat` maxes out at `2^(2^62)`

I see it maxing out just under half that: `2^(2^62-1)`

```julia
julia> big(2.0)^(2^62-2) * 2
Inf

julia> big(2.0)^(2^62-2) * 1.99999999999999
5.875653789111558236149365709100443785085523336097068334320279213651249557023989e+1388255822130839282

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 21, 2022, 1:55pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/6 "2022-07-21T13:55:42Z")

</div>

yeah, i was giving a somewhat rough number.

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [July 21, 2022, 3:15pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/7 "2022-07-21T15:15:30Z")

</div>

Maybe [LogarithmicNumbers](https://github.com/cjdoris/LogarithmicNumbers.jl) can help?

```julia
julia> m = exp(ULogarithmic, floatmax(BigFloat))
exp(5.875653789111587590936911998878442589938516392745498308333779606469323584389875e+1388255822130839282)

julia> log10(log10(m))
1.388255822130839282406840509258088745814167339601572288185265795534139656091031e+18

```

I think this should cover your case, but I get

```julia
julia> ten = ULogarithmic(big(10.0))
exp(2.302585092994045684017991454684364207601101488628772976033327900967572609677367)

julia> ten^(ten^100)
ERROR: StackOverflowError:
Stacktrace:
     [1] promote_type(#unused#::Type{BigFloat}, #unused#::Type{Logarithmic{BigFloat}})
       @ Base ./promotion.jl:298
     [2] promote_result(#unused#::Type, #unused#::Type, #unused#::Type{BigFloat}, #unused#::Type{Logarithmic{BigFloat}})
       @ Base ./promotion.jl:312
--- the last 2 lines are repeated 39990 more times ---
 [79983] promote_type(#unused#::Type{BigFloat}, #unused#::Type{Logarithmic{BigFloat}})
       @ Base ./promotion.jl:298

```

@cjdoris , do you have any ideas for addressing this?

EDIT: I added an issue: [Problems with big numbers · Issue #12 · cjdoris/LogarithmicNumbers.jl · GitHub](https://github.com/cjdoris/LogarithmicNumbers.jl/issues/12)

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [July 21, 2022, 3:34pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/8 "2022-07-21T15:34:33Z")

</div>

This feels like the right answer, but it does not work for my use case (`SpecialFunctions.loggamma`).

```julia
julia> loggamma(ULogarithmic(10)^(10^3))
Inf

julia> loggamma(big(10.0)^(10^18))
2.302585092994045683017991454684364207601101488628772976033327900967572609677336e+1000000000000000018

```

I’m not sure if this is a bug or an unavoidable artifact of the fact that these numbers are backed by only 64 bits.

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [July 21, 2022, 3:39pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/9 "2022-07-21T15:39:27Z")

</div>

`loggamma` is defined as

```julia
loggamma(x::Number) = _loggamma(float(x))

```

But in this case, `float(x)` returns `Inf`. I think you’ll need a new `loggamma` method to do this. Maybe you can modify the SpecialFunctions implementation for this type?

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [July 21, 2022, 3:59pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/10 "2022-07-21T15:59:04Z")

</div>

I think I’d use an approximation, and just make sure the error is small enough for your purposes. A good starting point is [Stirling’s approximation](https://en.wikipedia.org/wiki/Stirling%27s_approximation), and there are some alternatives in the links toward the bottom. It’s especially handy that the relative error decreases with the size of the argument, so you might end up needing only a couple of terms.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [July 21, 2022, 6:18pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/11 "2022-07-21T18:18:05Z")

</div>

The issue is that loggamma is defined via ccalls rather than wholly in Julia. If it were Julia all the way down, I would expect ULogarithmic’s implementations of primitives would make it work without modification.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [July 21, 2022, 7:35pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/12 "2022-07-21T19:35:54Z")

</div>

> [@Lilith](#):
>
> I don’t have enough memory to express them exactly as integers

Lol no way.

Here’s an implementation of Stirling’s approximation (the `oftype`s should not be required but work around the stack overflow reported above):

```julia
julia> using LogarithmicNumbers

julia> approxloggamma(x) = (
    x * oftype(x, log(x) - 1)
    - oftype(x, log(x) / 2)
    + 1/12x - 1/360x^3 + 1/1260x^5
)
approxloggamma (generic function with 1 method)

julia> approxloggamma(exp(ULogarithmic, 1e100))
+exp(1.0e100)

julia> setprecision(BigFloat, 1000)
1000

julia> approxloggamma(exp(ULogarithmic, big"1e100"))
+exp(1.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002302585092994045684017991454684364207601101488628772976033327900967572609677352480235997205089598298340967784042286248633409525465082806756666287369098781689482907208325554680843799894826233198528393505266e+100)

```

(Accurate to o(x^5) plus floating point precision.)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 21, 2022, 7:38pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/13 "2022-07-21T19:38:41Z")

</div>

```julia
approxloggamma(x::T) where {T} = (
    x * oftype(x, log(x) - 1)
    - oftype(x, log(x) / 2)
    + evalpoly(inv(x), (inv(T(12)), inv(T(360)), inv(T(1260)))
)

```

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [July 22, 2022, 8:39am UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/14 "2022-07-22T08:39:03Z")

</div>

In fact

```julia
approxloggamma(x) = x * oftype(x, log(x) - 1)

```

is correct to O(\log x), which is already far more accurate than the precision you’ll ever represent it to.

---

<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:** [July 22, 2022, 8:43pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/15 "2022-07-22T20:43:30Z")

</div>

What is the use case?

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [July 23, 2022, 8:05pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/16 "2022-07-23T20:05:32Z")

</div>

To quickly compute the limit as `n` goes to infinity of `2n*log(n) / log2(factorial(n))`. I was able to compute the value to 64-bit floating point precision, pasted it into Google, and found [this post](https://www.reddit.com/r/ProgrammerDadJokes/comments/rhus3j/why_is_a_java_programmer_afraid_of/) which led me to conclude that the closed form answer is `log(4)`. After getting this closed form, I continued on pencil and paper.

Beyond that already resolved use-case, this is a theoretical question.

---

<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:** [July 23, 2022, 8:13pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/17 "2022-07-23T20:13:29Z")

</div>

> [@Lilith](#):
>
> 2n\*log(n) / log2(factorial(n))

Wolfram Alpha is useful for this kind of query.

> **[limit(2n\*log(n) / log2(factorial(n)), n=infinity) - Wolfram|Alpha](https://www.wolframalpha.com/input?i=limit%282n*log%28n%29%2B%2F%2Blog2%28factorial%28n%29%29%2C%2Bn%3Dinfinity%29)**
>
> Wolfram|Alpha brings expert-level knowledge and capabilities to the broadest possible range of people—spanning all professions and education levels.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 23, 2022, 10:30pm UTC](https://discourse.julialang.org/t/representing-very-big-numbers/84589/18 "2022-07-23T22:30:40Z")

</div>

I haven’t bought the Pro so I cannot see the full solution, but it seems apparent that the limit was found by manipulating the math expression, not by actually computing values for large `n`. The largest `n` represented by all the software bits in the world is nothing compared to infinity, who knows if the value converges or diverges past there.
