title: Floating point inaccuracy during e calculation with numerical methods
body: <p>So i was calculating e(third row in picture) with numerical methods. </p>
  
  <p><a href="https://i.stack.imgur.com/pEdrK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pEdrK.png" alt="enter image description here"></a></p>
  
  <p>I was increasing the number of elements i used every iteration. And when i executed the program, floating point variable behaved in a way i didn't understand. Here is the program and the result.</p>
  
  <pre><code>import java.util.Scanner;
  
      public class Test {
  
         public static void main(String[] args) {
  
            Scanner input = new Scanner(System.in);
  
            int factorial = 1;
            int counter = 0;
            int iterationNumber;
            double total = 0;
            int tempCounter;
  
            System.out.print("Enter iteration number: ");
            iterationNumber = input.nextInt();
  
            while (counter &lt;= iterationNumber) {
  
               tempCounter = counter;
  
               while ((tempCounter - 1) &gt; 0) {
                  factorial *= tempCounter;
                  tempCounter--;
               }
  
               total += ((double)1 / factorial);
               System.out.println(total);
  
               factorial = 1;
               counter ++;
            }
         }
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Ac6aG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ac6aG.png" alt="enter image description here"></a></p>
  
  <p>So my question is why does the value of e starts to decrease after a while instead of increasing? I want to learn how floating point variable behaves during this program and the logic behind it. </p>
  
  <p>Another question is why does it start to say infinity?</p>