pocketlang/tests/examples/pi.pk

23 lines
417 B
Plaintext
Raw Normal View History

## PI approximation using Leibniz formula.
##
## PI/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
2022-04-22 00:07:38 +08:00
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
2021-06-12 14:56:09 +08:00
for i in 1..100000
sign *= -1
pi_by_4 += sign * 1/(2*i - 1)
end
2021-06-12 14:56:09 +08:00
pi = 4 * pi_by_4
assert(abs(pi - PI) < 0.00002)
print('PI =', pi)