Skip to content

Commit 7418ece

Browse files
committed
Add LICENSE, README and examples
1 parent 5e6dfca commit 7418ece

File tree

5 files changed

+452
-0
lines changed

5 files changed

+452
-0
lines changed

Diff for: LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Nick Craig-Wood
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Diff for: README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# gpython
2+
3+
gpython is a part re-implementation / part port of the Python 3.4
4+
interpreter to the Go language, "batteries not included".
5+
6+
It includes:
7+
8+
* runtime - using compatible byte code to python3.4
9+
* lexer
10+
* parser
11+
* compiler
12+
* interactive mode (REPL)
13+
14+
It does not include very many python modules as many of the core
15+
modules are written in C not python. The converted modules are:
16+
17+
* builtins
18+
* marshal
19+
* math
20+
* time
21+
* sys
22+
23+
## Objectives
24+
25+
Gpython was written as a learning experiment to investigate how hard
26+
porting Python to Go might be. It turns out that all those C modules
27+
are a significant barrier to making a fully functional port.
28+
29+
## Status
30+
31+
The project works well enough to parse all the code in the python 3.4
32+
distribution and to compile and run python 3 programs which don't
33+
depend on a module gpython doesn't support.
34+
35+
See the examples directory for some python programs which run with
36+
gpython.
37+
38+
Speed hasn't been a goal of the conversions however it runs pystone at
39+
about 20% of the speed of cpython. The pi test runs quicker under
40+
gpython as I think the Go long integer primitives are faster than the
41+
Python ones.
42+
43+
There are many directions this project could go in. I think the most
44+
profitable would be to re-use the
45+
[grumpy](https://github.com/google/grumpy) runtime (which would mean
46+
changing the object model). This would give access to the C modules
47+
that need to be ported and would give grumpy access to a compiler and
48+
interpreter (gpython does support `eval` for instance).
49+
50+
I (@ncw) haven't had much time to work on gpython (I started it in
51+
2013 and have worked on it very sporadically) so someone who wants to
52+
take it in the next direction would be much appreciated.
53+
54+
## Limitations and Bugs
55+
56+
Lots!
57+
58+
## Similar projects
59+
60+
* [grumpy](https://github.com/google/grumpy) - a python to go transpiler
61+
62+
## License
63+
64+
This is licensed under the MIT licence, however it contains code which
65+
was ported fairly directly directly from the cpython source code under
66+
the (PSF LICENSE)[https://github.com/python/cpython/blob/master/LICENSE)

Diff for: examples/pi_chudnovsky_bs.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""
2+
Python3 program to calculate Pi using python long integers, binary
3+
splitting and the Chudnovsky algorithm
4+
5+
See: http://www.craig-wood.com/nick/articles/ FIXME for explanation
6+
7+
Nick Craig-Wood <nick@craig-wood.com>
8+
"""
9+
10+
import math
11+
from time import time
12+
13+
def sqrt(n, one):
14+
"""
15+
Return the square root of n as a fixed point number with the one
16+
passed in. It uses a second order Newton-Raphson convgence. This
17+
doubles the number of significant figures on each iteration.
18+
"""
19+
# Use floating point arithmetic to make an initial guess
20+
floating_point_precision = 10**16
21+
n_float = float((n * floating_point_precision) // one) / floating_point_precision
22+
x = (int(floating_point_precision * math.sqrt(n_float)) * one) // floating_point_precision
23+
n_one = n * one
24+
while 1:
25+
x_old = x
26+
x = (x + n_one // x) // 2
27+
if x == x_old:
28+
break
29+
return x
30+
31+
32+
def pi_chudnovsky_bs(digits):
33+
"""
34+
Compute int(pi * 10**digits)
35+
36+
This is done using Chudnovsky's series with binary splitting
37+
"""
38+
C = 640320
39+
C3_OVER_24 = C**3 // 24
40+
def bs(a, b):
41+
"""
42+
Computes the terms for binary splitting the Chudnovsky infinite series
43+
44+
a(a) = +/- (13591409 + 545140134*a)
45+
p(a) = (6*a-5)*(2*a-1)*(6*a-1)
46+
b(a) = 1
47+
q(a) = a*a*a*C3_OVER_24
48+
49+
returns P(a,b), Q(a,b) and T(a,b)
50+
"""
51+
if b - a == 1:
52+
# Directly compute P(a,a+1), Q(a,a+1) and T(a,a+1)
53+
if a == 0:
54+
Pab = Qab = 1
55+
else:
56+
Pab = (6*a-5)*(2*a-1)*(6*a-1)
57+
Qab = a*a*a*C3_OVER_24
58+
Tab = Pab * (13591409 + 545140134*a) # a(a) * p(a)
59+
if a & 1:
60+
Tab = -Tab
61+
else:
62+
# Recursively compute P(a,b), Q(a,b) and T(a,b)
63+
# m is the midpoint of a and b
64+
m = (a + b) // 2
65+
# Recursively calculate P(a,m), Q(a,m) and T(a,m)
66+
Pam, Qam, Tam = bs(a, m)
67+
# Recursively calculate P(m,b), Q(m,b) and T(m,b)
68+
Pmb, Qmb, Tmb = bs(m, b)
69+
# Now combine
70+
Pab = Pam * Pmb
71+
Qab = Qam * Qmb
72+
Tab = Qmb * Tam + Pam * Tmb
73+
return Pab, Qab, Tab
74+
# how many terms to compute
75+
DIGITS_PER_TERM = math.log10(C3_OVER_24/6/2/6)
76+
N = int(digits/DIGITS_PER_TERM + 1)
77+
# Calclate P(0,N) and Q(0,N)
78+
P, Q, T = bs(0, N)
79+
one = 10**digits
80+
sqrtC = sqrt(10005*one, one)
81+
return (Q*426880*sqrtC) // T
82+
83+
# The last 5 digits or pi for various numbers of digits
84+
check_digits = (
85+
(100, 70679),
86+
(1000, 1989),
87+
(10000, 75678),
88+
(100000, 24646),
89+
(1000000, 58151),
90+
(10000000, 55897),
91+
)
92+
93+
if __name__ == "__main__":
94+
digits = 100
95+
pi = pi_chudnovsky_bs(digits)
96+
print(str(pi))
97+
for digits, check in check_digits:
98+
start =time()
99+
pi = pi_chudnovsky_bs(digits)
100+
print("chudnovsky_gmpy_bs: digits",digits,"time",time()-start)
101+
last_five_digits = pi % 100000
102+
if check == last_five_digits:
103+
print("Last 5 digits %05d OK" % last_five_digits)
104+
else:
105+
print("Last 5 digits %05d wrong should be %05d" % (last_five_digits, check))

0 commit comments

Comments
 (0)