title: Function Returning NaN
body: <p>I created a program that calculates this algorithm starting at k=1 and ending it k=100:</p>
  
  <p><img src="https://i.stack.imgur.com/sHIkH.png" alt="formula"></p>
  
  <p>Here's the code that I've created:</p>
  
  <pre><code>public static void calculatePi() {
      BigInteger firstFactorial;
      BigInteger secondFactorial;
      BigInteger firstMultiplication;
      BigInteger firstExponent;
      BigInteger secondExponent;
      int firstNumber = 1103;
      BigInteger firstAddition;
      double summationPi = 3.0;
      double currentPi = 3.0;
      double pi = 3.0;
      int secondNumber = 2;
      double thirdNumber = Math.sqrt(2.0);
      int fourthNumber = 9801;
      double prefix = 1;
  
      for(int i=1;i&lt;101;i++){
          firstFactorial = factorial(4*i);
          secondFactorial = factorial(i);
          firstMultiplication = BigInteger.valueOf(26390*i);
          firstExponent = exponent(secondFactorial, 4);
          secondExponent = exponent(BigInteger.valueOf(396),4*i);
          firstAddition = BigInteger.valueOf(firstNumber).add(firstMultiplication);
          summationPi = firstFactorial.intValue()*firstAddition.intValue();
          summationPi /= firstExponent.intValue()*secondExponent.intValue();
          currentPi += summationPi;
      }
  
      prefix = secondNumber*thirdNumber;
      prefix = prefix/fourthNumber;
  
      summationPi = summationPi*prefix;
  
      pi = 1/summationPi;
  
      System.out.println("Pi is: " + pi);
  
      return;
  }
  </code></pre>
  
  <p>The function exponent(a,b); returns the result of a^b. The function factorial(a) returns the factorial of a. I have proven that both of these functions work perfectly. However, the code seems to mysteriously be returning "NaN." I understand that this happens when something is divided by zero, however I have not been able to find any point at which something is divided by zero. Is there anything else that would cause this/I'm doing wrong?</p>
  
  <p>Note: In the for statement, I'm using i as k in the algorithm.</p>
  
  <p>Thanks in advance!</p>