Comments (4)
Just now, I tried to use math.isort() instead gmpy2. I recalculated and the last few digits became "8778"...
Even worse, when I print(pi[-1])
again, the output is 1...
I hate mathematics
update: I found online that the number 1000000th is actually 1
from gmpy.
The key issue in your code it the use of sqrt
versus isqrt
. You've been encountering precision errors with sqrt
. The following code should run correctly both with and without gmpy2
. You will need to comment out the gmpy2 import line and uncomment the following lines. See comments in code. I have verified calculations to both 1_000_000 and 10_000_000 digits.
def chudnovsky_binsplit_mpz(digits:int):
# Use just the next line only to use gmpy2.
from gmpy2 import mpz, isqrt
# Comment out the previous line and uncomment the following lines
# to use native Python. Limited to 10_000_000 digits.
# from math import isqrt
# import sys
# sys.set_int_max_str_digits(10_000_100)
# mpz = int
digits *= 2
def binsplit(a, b):
if b - a == 1:
if a == 0:
Pab = Qab = 1
else:
Pab = (6*a-5) * (2*a-1) * (6*a-1)
Qab = 640320**3//24 * a**3
Tab = (13591409 + 545140134 * a) * Pab
if a & 1:
Tab = -Tab
return mpz(Pab), mpz(Qab), mpz(Tab)
else:
m = (a + b) // 2
Pam, Qam, Tam = binsplit(a, m)
Pmb, Qmb, Tmb = binsplit(m, b)
Pab = Pam * Pmb
Qab = Qam * Qmb
Tab = Qmb * Tam + Pam * Tmb
return Pab, Qab, Tab
terms = digits // 14 + 1
P, Q, T = binsplit(0, terms)
# Use 'isqrt' (integer square root) instead of 'sqrt' (floating
# point square root).
#
# Remove redundant 'mpz(...)' call.
return Q * 426880 * isqrt(10005 * mpz(10)**digits) // T
def main():
n = 10_000_000
pi = str(chudnovsky_binsplit_mpz(n))
# Print exactly 'n' digits.
print(pi[:n])
if __name__ == "__main__":
main()
from gmpy.
Thank you for response. But when I run your code, the result is incorrect. I tried several times and eventually found that it lost accuracy when n >= 16383 (perhaps only on my computer).
Also, interestingly, if only print(pi[-1]), it is correct(when n = 1000000).
I think this goes far beyond my knowledge of programming. The purpose of this issue is only to remind you if there are any bugs in gmpy2. If I wrote the wrong code, you can close this issue.
In fact, I have given up on continuing to delve into calculating pi or computer processing large numbers, which is too difficult.
from gmpy.
Closing.
from gmpy.
Related Issues (20)
- Issue with large factorial HOT 6
- Access to gmplib.org
- Alternatives to using private API for conversion to/from CPython int's HOT 1
- Build & tests wheels for M$ Windows
- Executing advanced arithmetic operations --C++ to gmpy2 in Python HOT 1
- Is there an alternative for Decimal? HOT 5
- Please make a non-preview release with Python 3.12 support — thank you! HOT 18
- gmpy2 2.2.0a2 missing on PyPI HOT 6
- Windows build comments HOT 2
- Major issue in Linux wheels HOT 1
- Dead paper link in comments
- 2.2.0: change in tagging convention? 🤔 HOT 8
- gmpy2 Sieve Example Code Problem HOT 10
- Add .bit_count() to xmpz type
- Type declarations to allow integration with type checking and linting tools HOT 2
- Probably incorrect error message. HOT 9
- Invalid mpq.__float__?
- Intentional fallthroughs?
- Intentional fallthroughs? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gmpy.