How to solve this question by Excel The infinite series fn
How to solve this question by Excel?
The infinite series f(n) = sigma^n_i = 1 1/1^4 converges on a value of f(n) = pi^4/90 as n approaches infinity. Write a program in single precision to calculate f(n) for n = 10.000 by computing the sum from i = 1 to 10,000. Then repeat the calculation but in reverse order-that is, from i = 10.000 to 1 using increments of -1. In each case, compute the true percent relative error. Explain the results.Solution
Program:
 public class CalculatePiBy4 {
     static float forwardCalc () {
         float sum = 0;
         for (int i = 1; i <= 10000; i++){
             float pow = i*i*i*i;
             if (pow == 0)
                 continue;
             sum = (sum + 1/pow);
         }
         return sum;
     }
   
     static float reversedCalc () {
         float sum = 0;
         for (int i = 10000; i >= 1; i--){
             float pow = i*i*i*i;
             if (pow == 0)
                 continue;
             sum = (sum + 1/pow);
         }
         return sum;
     }
   
     public static void main (String[] args) {
         float f = forwardCalc();
         float r = reversedCalc();
         float test = (float)Math.pow(Math.PI, 4)/90;
         float fe = ((f-test)/test)*100;
         float re = ((r-test)/test)*100;
         System.out.println(\"forward = \"+f + \"    error = \"+ fe);
         System.out.println(\"reverse = \"+r + \"    error = \"+ re);
         System.out.println(\"test = \"+test);
     }
 }
Output:
forward = 1.0823172    error = -5.5071025E-4
 reverse = 1.0823189    error = -3.9651137E-4
 test = 1.0823232
 
 The Error in forward iteration is higher compared to reverse iteration because of the way single precision decimal values are handled:
 In forward iteration the sum value becomes 1 after the first iteration, after 64 iterations we add 1/644 ie. 10-24 in binary (that is after decimal point we have 24 zeros, then 1), but in Sinlge-precision floating number we can only store 23 bits in fraction, so for i>63 are all ignored in forward
 but in reverse we store 1/100004 first and slowly proceed towards 1, so how small may it be, the values are actually stored.
 This is beacuse of the technique used to store data in IEEE754 format. the binary equivalent of the number is normalized to a form with only single 1 before the decimal and 23 binary digits after the decimal. so when we add smaller number to bigger number, if the part after the floating point exceeds 23bits, it is ignored completely. So in forward Iteration for i>64, all values are not stored. but in reverse iteration, as I said we are increasing the value, hence after normalizing we actually no value is lost. U can execute the given code to see which values are ignored!
static float forwardCalc () {
         float sum = 0;
         for (int i = 1; i <= 10000; i++){
             float pow = i*i*i*i;
             if (pow == 0)
                 continue;
             float temp = sum;
             sum = (sum + 1/pow);
             if (temp == sum )
                 System.out.println(\"for ignore\" + i);
         }
         return sum;
     }
   
     static float reversedCalc () {
         float sum = 0;
         for (int i = 10000; i >= 1; i--){
             float pow = i*i*i*i;
             if (pow == 0)
                 continue;
             float temp = sum;
             sum = (sum + 1/pow);
             if (temp == sum )
                 System.out.println(\"rev ignore\" + i);
         }
         return sum;
     }


