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

I have written this piece of code in python.

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?..

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

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
4 Likes
2 Likes

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.)

5 Likes

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

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

julia> BigFloat(pi)
3.141592653589793238462643383279502884197169399375105820974944592307816406286198

julia> setprecision(1024)
1024

julia> BigFloat(pi)
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724586997

julia> setprecision(4096)
4096

julia> BigFloat(pi)
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429564
3 Likes

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 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 ?

2 Likes

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.

You have to set the precision you want. Similarly, you need to set the desired precision in Julia using setprecision if you want to use BigFloat (the default is about 77 decimal digits).

1 Like

For BigFloat, Julia calls the mpfr_const_pi function from the GNU MPFR library, whose implementation says it uses “a clever form of Brent-Salamin formula”.

For Float64/Float32/Float16, it just uses a hard-coded constant in the Julia source.

3 Likes