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?
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
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) - Ď
-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.)
Sorry for spamming the thread; hereâs Brentâs actual algorithm in seven lines:
## 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!
Hi,
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.