Giter Club home page Giter Club logo

Comments (4)

gpchn avatar gpchn commented on July 29, 2024

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.

casevh avatar casevh commented on July 29, 2024

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.

gpchn avatar gpchn commented on July 29, 2024

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.

casevh avatar casevh commented on July 29, 2024

Closing.

from gmpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.