How julia calculates pi?

I think I have found a simple formula to calculate pi but my formula is much slower than BigFloat (pi).
How julia calculates pi?

I also ask you: how do I know if my formula already exists?

For BigFloat(pi), Julia uses the MPFR C library’s mpfr_const_pi function. More details about Julia’s π-handling are in this 2017 post on the Julia Blog.


One way would be to scour the literature to see what algorithms are out there, you could probably start with the Wikipedia page on pi.

Another way, if you don’t have the training to do an exhaustive literature search, is to write your method up as clean as you can and post it on something like r/maths. Caution, though, if you lack that training the likelihood that your method is genuinely novel is vanishingly small. The way you’re formulating it here (“I came up with an algorithm, is it new?”) is more likely to get a supportive response than the more common “Here’s the new fastest way to do it and I’ve named it for myself”.


Can I post the formula here?


sure. why not?

@gustaphe said to post on reddit math.
It is not a question of the julia language.
(I signed up on reddit a few minutes ago and got blocked)

Post some Julia code that implements it and the math that the Julia code is based on. I don’t think anyone will complain. Maybe if you can, edit the topic to “offtopic”. As long as you’re Julia adjacent and honestly asking questions not pushy or confrontational I don’t think anyone is going to mind. Some people might even give you tips about how to improve the speed.


First one(π):

a = BigFloat(1) 
# you can also use other numbers 
# and you will converge to multiples of pi (or -pi, or 0)
a = a + sin(a) # 1.84147098480789650665250232163
a = a + sin(a) # 2.8050617093497299136094750235101
a = a + sin(a) # 3.1352763328997160003524035699574
a = a + sin(a) # 3.141592611590653149601161318361
a = a + sin(a) # and so on..... -> π

Second one (π/2 = 1.5707963267948966192313216916397):

a = BigFloat(1) 
# you can also use other numbers 
a = a + cos(a) # 1.5403023058681397174009366074441
a = a + cos(a) # 1.5707916010242611729993559629573
a = a + cos(a) # 1.5707963267948966016412881420516
a = a + cos(a) # and so on..... -> π/2

Third one (π):

a = BigFloat(3)  
# you can also use other numbers
a = a - tan(a) # 3.14254654307427780529563541053391
a = a - tan(a) # 3.14159265330047681544988577171991
a = a - tan(a) # and so on..... -> pi

Do these formulas exist?


These are what you’d call “fixed point iterations”, whether an iteration converges to a fixed point can be analyzed in terms of various properties of “iterative maps” which are studied in dynamical systems theory.


Thank you.
So these formulas already exist?

They are one example of a well known family of processes, yes.


Sind discussion here:


For those who are curious how it is implemented, you can read the source here: src/const_pi.c · master · mpfr / mpfr · GitLab


Sorry if I came across as rude. I just meant that you’ll get better answers if you choose a specific forum, it could have happened that nobody here knew the answer, while someone on r/maths could have pointed you in the right direction. You were in luck this time :slight_smile:


Interesting! It’s the Brent-Salamin formula.


This gets my hopes high that there will be an application for the geothmetic meandian one day


Indeed! Adapting a previous reference to this concept on this forum:

F(x) = [(x[1]+x[2])/2, √(x[1]x[2]), (x[1]-x[2])/2, x[1]^2 - x[2]^2]

Fⁿ(x,n) = ∘(fill(F,n)...)(x)

Q(n) = [Fⁿ([1,1/√BigFloat(2)],i) for i=1:n]

Pi(n) = (P = Q(n); 4*(P[n][1])^2/(1 - sum(2^(j)*P[j][4] for j=2:n)))

This function has impressive convergence:

julia> Pi(2) - π

julia> Pi(3) - π

julia> Pi(4) - π

julia> Pi(5) - π

julia> Pi(6) - π

julia> Pi(7) - π

(It exhausts the BigFloat precision after 6 iterations.)


Sorry for spamming the thread; here’s Brent’s actual algorithm in seven lines:

## Brent's method for calculation pi:
# Ref.:
# See page 10.
F(x) = [(x[1]+x[2])/2, √(x[1]x[2]), x[3] - x[4]*((x[1]-x[2])^2)/4, 2*x[4]]
compose(F,n) = ∘(fill(F,n)...)
function Pi(n)
    A, B, T, X = compose(F,n)([1,inv(sqrt(big(2))),1/4,1])
    return (A+B)^2/(4T)
# Compare:
Diff_to_π = [Pi(k) - big(π) for k in 1:5]

I’m done now!



I once looked into various ways of computing pi, I implemented some of those in Pi.jl. This was a while a ago, so the code might not be up to date with julia 1.0.