-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
452 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Nick Craig-Wood | ||
|
||
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: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# gpython | ||
|
||
gpython is a part re-implementation / part port of the Python 3.4 | ||
interpreter to the Go language, "batteries not included". | ||
|
||
It includes: | ||
|
||
* runtime - using compatible byte code to python3.4 | ||
* lexer | ||
* parser | ||
* compiler | ||
* interactive mode (REPL) | ||
|
||
It does not include very many python modules as many of the core | ||
modules are written in C not python. The converted modules are: | ||
|
||
* builtins | ||
* marshal | ||
* math | ||
* time | ||
* sys | ||
|
||
## Objectives | ||
|
||
Gpython was written as a learning experiment to investigate how hard | ||
porting Python to Go might be. It turns out that all those C modules | ||
are a significant barrier to making a fully functional port. | ||
|
||
## Status | ||
|
||
The project works well enough to parse all the code in the python 3.4 | ||
distribution and to compile and run python 3 programs which don't | ||
depend on a module gpython doesn't support. | ||
|
||
See the examples directory for some python programs which run with | ||
gpython. | ||
|
||
Speed hasn't been a goal of the conversions however it runs pystone at | ||
about 20% of the speed of cpython. The pi test runs quicker under | ||
gpython as I think the Go long integer primitives are faster than the | ||
Python ones. | ||
|
||
There are many directions this project could go in. I think the most | ||
profitable would be to re-use the | ||
[grumpy](https://github.com/google/grumpy) runtime (which would mean | ||
changing the object model). This would give access to the C modules | ||
that need to be ported and would give grumpy access to a compiler and | ||
interpreter (gpython does support `eval` for instance). | ||
|
||
I (@ncw) haven't had much time to work on gpython (I started it in | ||
2013 and have worked on it very sporadically) so someone who wants to | ||
take it in the next direction would be much appreciated. | ||
|
||
## Limitations and Bugs | ||
|
||
Lots! | ||
|
||
## Similar projects | ||
|
||
* [grumpy](https://github.com/google/grumpy) - a python to go transpiler | ||
|
||
## License | ||
|
||
This is licensed under the MIT licence, however it contains code which | ||
was ported fairly directly directly from the cpython source code under | ||
the (PSF LICENSE)[https://github.com/python/cpython/blob/master/LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
""" | ||
Python3 program to calculate Pi using python long integers, binary | ||
splitting and the Chudnovsky algorithm | ||
See: http://www.craig-wood.com/nick/articles/ FIXME for explanation | ||
Nick Craig-Wood <[email protected]> | ||
""" | ||
|
||
import math | ||
from time import time | ||
|
||
def sqrt(n, one): | ||
""" | ||
Return the square root of n as a fixed point number with the one | ||
passed in. It uses a second order Newton-Raphson convgence. This | ||
doubles the number of significant figures on each iteration. | ||
""" | ||
# Use floating point arithmetic to make an initial guess | ||
floating_point_precision = 10**16 | ||
n_float = float((n * floating_point_precision) // one) / floating_point_precision | ||
x = (int(floating_point_precision * math.sqrt(n_float)) * one) // floating_point_precision | ||
n_one = n * one | ||
while 1: | ||
x_old = x | ||
x = (x + n_one // x) // 2 | ||
if x == x_old: | ||
break | ||
return x | ||
|
||
|
||
def pi_chudnovsky_bs(digits): | ||
""" | ||
Compute int(pi * 10**digits) | ||
This is done using Chudnovsky's series with binary splitting | ||
""" | ||
C = 640320 | ||
C3_OVER_24 = C**3 // 24 | ||
def bs(a, b): | ||
""" | ||
Computes the terms for binary splitting the Chudnovsky infinite series | ||
a(a) = +/- (13591409 + 545140134*a) | ||
p(a) = (6*a-5)*(2*a-1)*(6*a-1) | ||
b(a) = 1 | ||
q(a) = a*a*a*C3_OVER_24 | ||
returns P(a,b), Q(a,b) and T(a,b) | ||
""" | ||
if b - a == 1: | ||
# Directly compute P(a,a+1), Q(a,a+1) and T(a,a+1) | ||
if a == 0: | ||
Pab = Qab = 1 | ||
else: | ||
Pab = (6*a-5)*(2*a-1)*(6*a-1) | ||
Qab = a*a*a*C3_OVER_24 | ||
Tab = Pab * (13591409 + 545140134*a) # a(a) * p(a) | ||
if a & 1: | ||
Tab = -Tab | ||
else: | ||
# Recursively compute P(a,b), Q(a,b) and T(a,b) | ||
# m is the midpoint of a and b | ||
m = (a + b) // 2 | ||
# Recursively calculate P(a,m), Q(a,m) and T(a,m) | ||
Pam, Qam, Tam = bs(a, m) | ||
# Recursively calculate P(m,b), Q(m,b) and T(m,b) | ||
Pmb, Qmb, Tmb = bs(m, b) | ||
# Now combine | ||
Pab = Pam * Pmb | ||
Qab = Qam * Qmb | ||
Tab = Qmb * Tam + Pam * Tmb | ||
return Pab, Qab, Tab | ||
# how many terms to compute | ||
DIGITS_PER_TERM = math.log10(C3_OVER_24/6/2/6) | ||
N = int(digits/DIGITS_PER_TERM + 1) | ||
# Calclate P(0,N) and Q(0,N) | ||
P, Q, T = bs(0, N) | ||
one = 10**digits | ||
sqrtC = sqrt(10005*one, one) | ||
return (Q*426880*sqrtC) // T | ||
|
||
# The last 5 digits or pi for various numbers of digits | ||
check_digits = ( | ||
(100, 70679), | ||
(1000, 1989), | ||
(10000, 75678), | ||
(100000, 24646), | ||
(1000000, 58151), | ||
(10000000, 55897), | ||
) | ||
|
||
if __name__ == "__main__": | ||
digits = 100 | ||
pi = pi_chudnovsky_bs(digits) | ||
print(str(pi)) | ||
for digits, check in check_digits: | ||
start =time() | ||
pi = pi_chudnovsky_bs(digits) | ||
print("chudnovsky_gmpy_bs: digits",digits,"time",time()-start) | ||
last_five_digits = pi % 100000 | ||
if check == last_five_digits: | ||
print("Last 5 digits %05d OK" % last_five_digits) | ||
else: | ||
print("Last 5 digits %05d wrong should be %05d" % (last_five_digits, check)) |
Oops, something went wrong.