parallel assignment variable from python to java (Pi algorithm)
Question: I would like to translate a python algorithm to Java, I have this source code
(using parallel asignment variable (doesn't exist in Java :( )

    
    
    # -*- coding: cp1252 -*-
    #! /usr/bin/env python
    
    import sys
    
    
    
    def main():
        k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
        while 1:
            p, q, k = k*k, 2L*k+1L, k+1L
            a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
            d, d1 = a/b, a1/b1
            while d == d1:
                output(d)
                a, a1 = 10L*(a%b), 10L*(a1%b1)
                d, d1 = a/b, a1/b1
    
    def output(d):
        sys.stdout.write(`int(d)`)
        sys.stdout.flush()
        #ecriture en continue du chiffre
        pi = open("flypi.html", "a")
        pi.write(`int(d)`)
        pi.write("\n")
        pi.close()
    
    
    main()
    

So, first I recoded the same script without parallel assignement variable :

    
    
    # -*- coding: cp1252 -*-
    #! /usr/bin/env python
    
    import sys
    
    
    
    def main():
        #k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
        k = 2L
        a = 4L
        b = 1L
        a1 = 12L
        b1 = 4L
    
        while 1:
    
            #p, q, k = k*k, 2L*k+1L, k+1L
            kk = k
            p = kk*kk
            q = 2L*kk+1L
            k = kk+1L
    
            #a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
            aa = a
            bb = b
            a = a1
            b = b1
            a1 = p*aa+q*a1
            b1 = p*bb+q*b1
    
            #d, d1 = a/b, a1/b1
            d = a/b
            d1 = a1/b1
            while d == d1:
                output(d)
    
                #a, a1 = 10L*(a%b), 10L*(a1%b1)
                a = 10L*(a%b)
                a1 = 10L*(a1%b1)
    
                #d, d1 = a/b, a1/b1
                d = a/b
                d1 = a1/b1
    
    def output(d):
        sys.stdout.write(`int(d)`)
        sys.stdout.flush()
        #ecriture en continue du chiffre
        pi = open("flypi.html", "a")
        pi.write(`int(d)`)
        pi.write("\n")
        pi.close()
    
    
    main()
    

The output of these two script is the same:

    
    
    31415926535897932384626433832795028841971693993751058209749445923078164062862089 (crt+c)
    

Now here is the script I made in Java (almost the same as the second python
script):

    
    
    public static void cal(){
        //int i = 0;
        long d = 0;
        long k = 2L;
        long a = 4L;
        long b = 1L, a1 = 12L, b1 = 4L;
        long p = 0, q = 0, d1 = 0;
    
        long aa = 0, bb = 0;
        long kk = 0;
    
    
        while(true){
    
            kk = k;
            p = kk*kk;
            q = 2L*kk+1L;
            k = kk+1L;
    
            aa = a;
            bb = b;
            a = a1;
            b = b1;
            a1 = p*aa+q*a1;
            b1 = p*bb+q*b1;
    
            d = a/b;
            d1 = a1/b1;
            while(d == d1){
                System.out.print(d);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                a = 10L*(a%b);
                a1 = 10L*(a1%b1);
    
                d = a/b;
                d1 = a1/b1;
            }
            //i++;
    
        }
    }
    

but the output is wrong :

    
    
    31415926530000000001-100000000000000000100-300000101000000000000000000000000000000000000 (ctr+c)
    

Thank you, and sorry for the long post :)

EDIT: So yes it's a buffer overflow. I tried to implement BigInteger and it
works fine !! thank you !


Answer: In Python integers can be arbitrarily large. In Java a long consists of 64
bits and can therefore only store numbers smaller than about 2**64 / 2.

If a number is too big, the first bits of it are discarded and the most
significant bit that is not overwrites the sign of the integer, resulting in
negative numbers in mathematically impossible places.

Use BigInteger as ajb suggests or change your calculations in some way.

