pocketlang/tests/examples/pi.pk
2022-04-22 07:04:09 +05:30

23 lines
417 B
Plaintext

## PI approximation using Leibniz formula.
##
## PI/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
from math import abs
## Temproarly we cannot register variables on native modules
## will be implemented soon. So defining the PI here.
PI = 3.14159265358979323846
pi_by_4 = 0; sign = -1
for i in 1..100000
sign *= -1
pi_by_4 += sign * 1/(2*i - 1)
end
pi = 4 * pi_by_4
assert(abs(pi - PI) < 0.00002)
print('PI =', pi)