Skip to content

Commit d44dbe3

Browse files
committed
Create upi.py
1 parent 53c45e5 commit d44dbe3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

boards/bearpi/pi/upi.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
3+
BEARPI pi demo
4+
5+
'''
6+
7+
from utime import ticks_ms, ticks_diff
8+
9+
def pi(places=1000):
10+
# 3 + 3*(1/24) + 3*(1/24)*(9/80) + 3*(1/24)*(9/80)*(25/168)
11+
# The numerators 1, 9, 25, ... are given by (2x + 1) ^ 2
12+
# The denominators 24, 80, 168 are given by (16x^2 -24x + 8)
13+
extra = 8
14+
one = 10 ** (places+extra)
15+
t, c, n, na, d, da = 3*one, 3*one, 1, 0, 0, 24
16+
17+
while t > 1:
18+
n, na, d, da = n+na, na+8, d+da, da+32
19+
t = t * n // d
20+
c += t
21+
return c // (10 ** extra)
22+
23+
def pi_t(n=1000):
24+
t1 = ticks_ms()
25+
r = pi(n)
26+
t2 = ticks_ms()
27+
print('Calculation time:', ticks_diff(t2, t1), 'ms')
28+
print('Result:', r)
29+
30+
pi_t(1000)

0 commit comments

Comments
 (0)