# How julia calculates pi?

**URL:** <https://discourse.julialang.org/t/how-julia-calculates-pi/87094>\
**Category:** Offtopic\
**Created:** [September 11, 2022, 7:09pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094 "2022-09-11T19:09:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 11, 2022, 7:09pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/1 "2022-09-11T19:09:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 11, 2022, 7:43pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/2 "2022-09-11T19:43:47Z")

</div>

For `BigFloat(pi)`, Julia uses the MPFR C library’s [mpfr\_const\_pi](https://www.mpfr.org/mpfr-current/mpfr.html#index-mpfr_005fconst_005fpi) function. More details about Julia’s π-handling are in [this 2017 post](https://julialang.org/blog/2017/03/piday/) on the Julia Blog.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 11, 2022, 8:22pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/3 "2022-09-11T20:22:07Z")

</div>

> [@qwerty](#):
>
> how do I know if my formula already exists?

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

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 11, 2022, 8:33pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/4 "2022-09-11T20:33:33Z")

</div>

Can I post the formula here?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 11, 2022, 8:55pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/5 "2022-09-11T20:55:57Z")

</div>

sure. why not?

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 11, 2022, 8:57pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/6 "2022-09-11T20:57:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 11, 2022, 8:59pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/7 "2022-09-11T20:59:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 11, 2022, 9:19pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/8 "2022-09-11T21:19:22Z")

</div>

First one(π):

```julia
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):

```julia
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 (π):

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

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 11, 2022, 9:24pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/9 "2022-09-11T21:24:01Z")

</div>

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.

> **[Iterated function](https://en.wikipedia.org/wiki/Iterated_function)**
>
> In mathematics, an iterated function is a function X → X (that is, a function from some set X to itself) which is obtained by composing another function f : X → X with itself a certain number of times. The process of repeatedly applying the same function is called iteration. In this process, starting from some initial object, the result of applying a given function is fed again in the function as input, and this process is repeated. For example on the image on the right:
> Iterated functions ar...

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 11, 2022, 9:25pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/10 "2022-09-11T21:25:49Z")

</div>

Thank you.  
So these formulas already exist?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 11, 2022, 9:26pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/11 "2022-09-11T21:26:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 12, 2022, 5:49am UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/12 "2022-09-12T05:49:25Z")

</div>

Sind discussion here:

> <https://math.stackexchange.com/questions/3785584/iterative-algorithm-for-pi>

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 12, 2022, 9:16am UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/13 "2022-09-12T09:16:57Z")

</div>

> [@digital\_carver](#):
>
> Julia uses the MPFR C library’s [mpfr\_const\_pi](https://www.mpfr.org/mpfr-current/mpfr.html#index-mpfr_005fconst_005fpi) function.

For those who are curious how it is implemented, you can read the source here: [src/const\_pi.c · master · mpfr / mpfr · GitLab](https://gitlab.inria.fr/mpfr/mpfr/-/blob/master/src/const_pi.c#L41-122)

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 12, 2022, 11:06am UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/14 "2022-09-12T11:06:37Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [September 13, 2022, 5:00am UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/15 "2022-09-13T05:00:17Z")

</div>

Interesting! It’s the [Brent-Salamin formula](https://mathworld.wolfram.com/Brent-SalaminFormula.html).

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [September 13, 2022, 6:42am UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/16 "2022-09-13T06:42:11Z")

</div>

This gets my hopes high that there will be an application for the [geothmetic meandian](https://xkcd.com/2435/) one day

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [September 13, 2022, 11:16am UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/17 "2022-09-13T11:16:41Z")

</div>

Indeed! Adapting a [previous reference](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/68) to this concept on this forum:

```julia
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
julia> Pi(2) - π
-0.001013403067624990151312114303679572423729161862157485331457898962233648251246769

julia> Pi(3) - π
-7.376250956313298951296807109882732176029503026415405107486381639065704112971525e-09

julia> Pi(4) - π
-1.831306084776389098159438945396588851876138902814342684477531468625531008649221e-19

julia> Pi(5) - π
-5.472109145689941832748533178964178489138195886835923610924029609286031340771535e-41

julia> Pi(6) - π
3.109020679834000065139086670608143845601760131197061298608533249260693049138474e-76

julia> Pi(7) - π
3.109020679834000065139086670608143845601760131197061298608533249260693049138474e-76

```

(It exhausts the `BigFloat` precision after 6 iterations.)

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [September 15, 2022, 1:04pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/18 "2022-09-15T13:04:11Z")

</div>

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

```julia
## Brent's method for calculation pi:
# Ref.: https://maths-people.anu.edu.au/~brent/pd/rpb028.pdf
# 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)
end
# Compare:
Diff_to_π = [Pi(k) - big(π) for k in 1:5]

```

I’m done now!

---

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [September 18, 2022, 8:22am UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/19 "2022-09-18T08:22:25Z")

</div>

Hi,

I once looked into various ways of computing pi, I implemented some of those in [Pi.jl](https://github.com/davidavdav/Pi.jl). This was a while a ago, so the code might not be up to date with julia 1.0.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [March 14, 2026, 1:02am UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/20 "2026-03-14T01:02:04Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.
