# Is there any package similar to python's decimal in Julia? Where we can use any precision?

**URL:** <https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834>\
**Category:** Performance\
**Tags:** question, package, numbers, numerics\
**Created:** [September 27, 2021, 8:28pm UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834 "2021-09-27T20:28:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![aburousan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aburousan/32/28525_2.png) [@aburousan](https://discourse.julialang.org/u/aburousan)\
**Post date:** [September 27, 2021, 8:28pm UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/1 "2021-09-27T20:28:56Z")

</div>

I have written this piece of code in python.

```nohighlight
from decimal import *
def pi_chudn(n):
    getcontext().prec = n+50
    k=0
    pi_chud = 0
    while k<n:
        pi_chud+=(((Decimal(-1))**k ) * (Decimal(mp.factorial(6*k)))*(13591409 + 545140134*k))/Decimal((mp.factorial(3*k)*((mp.factorial(k))**3)*(640320**((3*k)+(Decimal(1.5))))))
        k+=1
    pi_chud = (Decimal(pi_chud) * 12)
    pi_chud = (Decimal(pi_chud**(-1)))
    return int(pi_chud*10**n)

exact_pi_val = str(31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989)
for n in range(1,1000):
    print(int(exact_pi_val[:n+1]))
    print(pi_chudn(n))
    is_true = (pi_chudn(n) == int(exact_pi_val[:n+1]))
    print("for n = ",n, " It is ",is_true)
    if is_true == False:
    	break

```

I was wondering if I can do that same in Julia too? But I don’t know if there is any pakage in Julia which is similar to decimal and which has Decimal function?  
Can someone please help with this?..

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [September 27, 2021, 8:42pm UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/2 "2021-09-27T20:42:11Z")

</div>

I think that the answer is the built-in `BigFloat` type, e.g.

```julia
julia> 0.1 + 0.1 + 0.1 - 0.3 # with "standard" Float64
5.551115123125783e-17

julia> big"0.1" + big"0.1" + big"0.1" - big"0.3" # with BigFloat
0.0

```

---

<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:** [September 27, 2021, 9:13pm UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/3 "2021-09-27T21:13:31Z")

</div>

> **[Decimals.jl](https://www.juliapackages.com/p/decimals)**
>
> Pure Julia decimal arithmetic library.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 28, 2021, 1:12am UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/4 "2021-09-28T01:12:57Z")

</div>

If you are computing pi, or similar quantities, then you don’t need to exactly represent decimal values and I would just use Julia’s built-in BigFloat type. (Even in Python, you might want to use mpmath instead of decimal.)

---

<div class="post-metadata">

**Author:** ![aburousan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aburousan/32/28525_2.png) [@aburousan](https://discourse.julialang.org/u/aburousan)\
**Post date:** [September 28, 2021, 2:47am UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/5 "2021-09-28T02:47:57Z")

</div>

@stevengj thank you… Although I have tried mpmath but it only gives me accuracy upto 16 decimal terms for pi.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [September 28, 2021, 3:06am UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/6 "2021-09-28T03:06:14Z")

</div>

I’m not sure what your aiming to accomplish, but Julia’s BigFloat is pretty easy and powerful:

```julia
julia> BigFloat(pi)
3.141592653589793238462643383279502884197169399375105820974944592307816406286198

julia> setprecision(1024)
1024

julia> BigFloat(pi)
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724586997

julia> setprecision(4096)
4096

julia> BigFloat(pi)
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429564

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [September 28, 2021, 8:14am UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/7 "2021-09-28T08:14:03Z")

</div>

just out of curiosity, how pi is numerically computed in Julia? Is there somewhere a constant with a given precision ?

ok, I have read [here](https://www.scienceabc.com/pure-sciences/how-do-computers-calculate-the-digits-of-pi.html) that you can use some infinite series to approximate pi with the desired accuracy… my best guess is that there is a constant defined up to a certain precision, and if you want more the value is computed on the flight using one of the infinite series approximation method… right ?

---

<div class="post-metadata">

**Author:** ![aburousan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aburousan/32/28525_2.png) [@aburousan](https://discourse.julialang.org/u/aburousan)\
**Post date:** [September 28, 2021, 8:24am UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/8 "2021-09-28T08:24:48Z")

</div>

Yes we use infinite series but most of them convergence very slowly. The programme given above actually uses one of the most efficient algorithm called “Chudnovsky algorithm”. In this series just the first term gives 7-8 digits of pi if I remember correctly. And as per as I know this type of series are used in computers.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 28, 2021, 11:59am UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/9 "2021-09-28T11:59:43Z")

</div>

> [@aburousan](#):
>
> Although I have tried mpmath but it only gives me accuracy upto 16 decimal terms for pi.

You have to [set the precision](https://mpmath.org/doc/current/basics.html#setting-the-precision) you want. Similarly, you need to [set the desired precision in Julia](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Arbitrary-Precision-Arithmetic) using `setprecision` if you want to use `BigFloat` (the default is about 77 decimal digits).

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 28, 2021, 12:09pm UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/10 "2021-09-28T12:09:55Z")

</div>

> [@sylvaticus](#):
>
> my best guess is that there is a constant defined up to a certain precision

> <https://github.com/JuliaLang/julia/blob/bb5b98e72a151c41471d8cc14cacb495d647fb7f/base/mathconstants.jl#L13>

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 28, 2021, 12:12pm UTC](https://discourse.julialang.org/t/is-there-any-package-similar-to-pythons-decimal-in-julia-where-we-can-use-any-precision/68834/11 "2021-09-28T12:12:11Z")

</div>

> [@sylvaticus](#):
>
> just out of curiosity, how pi is numerically computed in Julia? Is there somewhere a constant with a given precision ?

For `BigFloat`, [Julia calls](https://github.com/JuliaLang/julia/blob/84cc901218c673d6c6a1170cec4302ab59aaaa7d/base/irrationals.jl#L177) the `mpfr_const_pi` function from the GNU MPFR library, [whose implementation](https://fossies.org/linux/mpfr/src/const_pi.c) says it uses “a clever form of [Brent-Salamin formula](https://mathworld.wolfram.com/Brent-SalaminFormula.html)”.

For `Float64`/`Float32`/`Float16`, it just uses a [hard-coded constant](https://github.com/JuliaLang/julia/blob/84cc901218c673d6c6a1170cec4302ab59aaaa7d/base/mathconstants.jl#L13) in the Julia source.
