Java Help?

Discussion in 'Miscellaneous' started by Gibabyte, Jan 14, 2016.

?

Do you know how to use any programming languages?

Java or JavaScript 5 vote(s) 83.3%
Python 4 vote(s) 66.7%
One or more in the C family 4 vote(s) 66.7%
HTML 3 vote(s) 50.0%
PHP 1 vote(s) 16.7%
VisualBasic 1 vote(s) 16.7%
I don't code =P 0 vote(s) 0.0%
Multiple votes are allowed.
  1. Hello (hopefully) experienced java programmers. I was messing around with a program I wrote today, bored in french class, and I was having trouble determining why it was resulting in an infinite loop. I couldn't find anything online, but I'm sure someone here can help me figure it out. ;)

    Code:
    package piLibraries;
    import java.util.Scanner;
    public class Pi
    {
    	public static void main(String[] args)
    	{
    		boolean debug = false;
    		Scanner s = new Scanner(System.in);
    		System.out.println("How many iterations should I complete?");
    	        long iterations = s.nextLong();
    		System.out.println("Working...");
    		double denominator = 1.0; 
    		double finPi = 4 / denominator;
    		double it1, it2;
    		if (!debug) 
    		{
    			for (int count = 0; count <= iterations; count ++)
    		
    			{
    			denominator += 2;
    			it1 = finPi - 4 / denominator;
    			denominator += 2;
    			it2 = it1 + 4 / denominator;
    			finPi = it2;
    			if (count % 1000000000 == 0)
    			{
    				System.out.println("The current approximation is " + finPi);
    				System.out.println("You are at " + count + " iteratios out of " + iterations + ".");
    				System.out.println("======================================");
    			}
    			}
    		}
    		long output = (long)finPi;
    		System.out.println("Pi is about " + output + ".");
    
    	}
    
    }
    /*
    Okay, so this is the issue. It generates an infinite loop that goes to negative instead of counting normally. 
    Is there something I'm missing here?
    Here is the output:
    How many iterations should I complete?
    100000000000
    Working...
    The current approximation is 3.466666666666667
    You are at 0 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.1415926540880768
    You are at 1000000000 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.1415926538379395
    You are at 2000000000 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.1415926538059558
    You are at -2000000000 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.1415926537399748
    You are at -1000000000 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.141592653704704
    You are at 0 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.1415926536827814
    You are at 1000000000 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.141592653667801
    You are at 2000000000 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.1415926536642726
    You are at -2000000000 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.141592653654214
    You are at -1000000000 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.1415926536465064
    You are at 0 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.1415926536405
    You are at 1000000000 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.141592653635618
    You are at 2000000000 iteratios out of 100000000000.
    ======================================
    The current approximation is 3.1415926536343335
    You are at -2000000000 iteratios out of 100000000000.
    ======================================
    
    
    Et Cetera.
    */
    
    Please do ignore my inefficient and slightly sloppy coding =P
  2. I'm still looking but right off, I would say you need to use a long for count instead of int.

    --------------------------------------------------------

    I see one other thing unrelated to the infinite loop that you might want to think about.

    Currently you are adding two terms for each iteration of count, which I do not think you intended since you named two of your variables it1 & it2 which you are using to add or subtract alternating terms.

    You could instead use a variable to cause the alternating terms to be either added or subtracted. Something like:

    finPi += sign * (4 / denominator);
    denominator += 2;
    sign*=-1;
    TsuriNeko likes this.
  3. As Pab10S said, in your for loop, you simply just need to change int to long. The problem being, the number you entered for iterations was 10^11, but the maximum amount that an int can go up to is 2.14x10^9 (1x10^11 > 2.14x10^9). Anyway, the for loop is going to try to continue looping past the int's maximum range, because the syntax is technically correct, but when it reaches the int's maximum range, the int's value will roll over to -2.14x10^9. This is why the value ends up as a negative, also creating an infinite loop.
    In short, you just need to change 'int' to 'long' in your for loop because of range reasons.
    (Also, I think we all knew that pi was about 3, just saying).
    [EDIT]: Fixed some numbers.
    TsuriNeko likes this.