pocketlang/tests/examples/pi.pk
Thakee Nathees b5d98ca4c3 adding globals support in native interface
pkSetGlobal function implemented to support adding globals from the
native interface. Also the PKVM slots doesn't need a runtime anymore to
use them (see math.PI global to see how it works).
2022-05-09 22:52:25 +05:30

19 lines
277 B
Plaintext

## PI approximation using Leibniz formula.
##
## PI/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
from math import abs, PI
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)