Post Reply 
[VA] SRC#006- Pi Day 2020 Special: A New Fast Way to Compute Pi
03-16-2020, 11:15 AM (This post was last modified: 03-16-2020 01:13 PM by J-F Garnier.)
Post: #13
RE: [VA] SRC#006- Pi Day 2020 Special: A New Fast Way to Compute Pi
(03-15-2020 06:08 PM)Albert Chan Wrote:  Slightly optimized version, using less operations.

Code:
10 FNM(X)=X*(5-X*X*(4+16*(1-X)*(1+X)))
20 FNT(X)=X*(1-X*X*(20-X*X)/120)
30 FNS(X)=FNM(FNM(FNM(FNT(.008*X))))
40 FNP(X)=X+FNS(X)

>FNP(3)
3.14112000806
>FNP(RES)
3.14159265358

Ok, now let's try the next iteration, on the 35-digits Decimal Free42.
The program is easily ported to RPN, thanks to Albert's structured code:

Code:
# FNM(X)=X*(5-X*X*(4+16*(1-X)*(1+X)))
lbl fnm
1  rcl y  -
1  lastx  +  *
16  *
4  +
rcl y  x^2  *
5  x<>y  -
*
rtn

# FNT(X)=X*(1-X*X/6*(1-X*X/20))
lbl fnt
rcl y  x^2 
rcl x  20  /
1  x<>y  -

6  /
1  x<>y  -
*
rtn

# FNS(X)=FNM(FNM(FNM(FNT(.008*X)))
lbl fns
125  /
xeq fnt
xeq fnm
xeq fnm
xeq fnm
rtn

# FNP(X)=X+FNS(X)
lbl fnp
sto 00
xeq fns
rcl 00
+
3
xeq fnp
3,141120008059...
xeq fnp
3,14159265357...
xeq fnp
3,141592653589[636096612966527996358]

13 correct digits. Not accurate enough.

Let's try with 5 calls to fnm:
Code:
lbl fns
625 /
xeq fnt
xeq fnm
xeq fnm
xeq fnm
xeq fnm
rtn

3
xeq fnp
xeq fnp
xeq fnp
3,1415926535897932[28408529447963301]

Still not accurate enough.

Let's try to add 1 term in the sin expansion:
Code:
# FNT(X)=X*(1-X*X/6*(1-X*X/20*(1-X*X/42)))
lbl fnt
rcl x  x^2      
rcl x  42  /      
1  x<>y  -
rcl y  *  20  /
1  x<>y  -

6  /
1  x<>y  -
*
rtn
3
xeq fnp
xeq fnp
xeq fnp
3,14159265358979323846264[6911462229]

24 correct digits. Still not accurate enough.

Let's try to add another term in the sin expansion:
Code:
# FNT(X)=X*(1-X*X/6*(1-X*X/20*(1-X*X/42*(1-X*X/72) )))
lbl fnt
rcl x  x^2  
rcl x  72  / 
1  x<>y  -
rcl y  *  42  / 
1  x<>y  -
rcl y  *  20  /
1  x<>y  -

6  /
1  x<>y  -
*
rtn
3
xeq fnp
xeq fnp
xeq fnp
3,14159265358979323846264338327[8692]

30 correct digits. Let's stop here.

Total 9 + 5*5 = 34 mult/div operations, with optimizations (avoiding to calculate x*x several times)

J-F
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: [VA] SRC#006- Pi Day 2020 Special: A New Fast Way to Compute Pi - J-F Garnier - 03-16-2020 11:15 AM



User(s) browsing this thread: 1 Guest(s)