I have written code in C to calculate the geometric series for three different cases. The values for the three cases are as follows.
case 1:
n=10000,a=2.0,r=0.01.
Case 2:
n=500,a=0.01,r=1.1.
Case 3:
n=100,a= 0.0001, r = 2.0.
The geometric series has the following formula
Sn = a+ar^1 +ar^2 +ar^3 +···ar^n = a(1 - r^(n +1))/(1 - r)
In my code I have calculated both formulas. The left one in my code is called summation and the right is formula.
In the first case my code returns the right values but for the second and third case my returns huge numbers. I dont know what is going wrong since the code for the first case is the same as the second and third.
Can someone tell me why for the second and third case I am getting large numbers and how I can fix this?
The output I get for the cases are as follows:
======== Switch Case =========.
Case 1 summation = 2.020202
Case 1 formula = 2.020202
==============================.
Case 2 summation = 54668261640434936064.000000.
Case 2 formula = 54668261640434936060.000000
==============================.
Case 3 summation = 253530120045645892448944128.000000
Case 3 formula = 253530120045645892448944128.000000
==============================.
=======End of Switch Case=======.
my code is the following
#include <stdio.h>
#include <math.h>
int main(){
// This script is for the geometric series
long double n = 0, a = 0, r = 0;
long double summation = 0, formula = 0;
printf("==============================\n");
printf("======== Switch Case =========\n");
for(int i = 1; i <= 3; i++ ){
// Switch case for three scenarios
switch (i){
case 1:
n = 10000, a = 2.0L, r = 0.01L;
summation = a;
printf("%.6Lf \n",summation);
for(int j = 1; j <= n; j++){
summation += a*powl(r,j);
}
formula = a*(1 - powl(r,n+1))/(1 - r);
printf("Case 1 summation = %.6Lf \n",summation);
printf("Case 1 formula = %.6Lf \n",formula);
printf("==============================\n");
break;
case 2:
n = 500, a = 0.01L, r = 1.1L;
summation = a;
printf("%.6Lf \n",summation);
printf("%.6Lf \n",formula);
for(int j = 1; j <= n; j++){
summation += a*powl(r,j);
}
formula = a*(1 - powl(r,n+1))/(1 - r);
printf("Case 2 summation = %.6Lf \n",summation);
printf("Case 2 formula = %.6Lf \n",formula);
printf("==============================\n");
break;
case 3:
n = 100, a = 0.0001, r = 2.0;
summation = a;
printf("%.6Lf \n",summation);
for(int j = 1; j <= n; j++){
summation += a*powl(r,j);
}
formula = a*(1 - powl(r,n+1))/(1 - r);
printf("Case 3 summation = %.6Lf \n",summation);
printf("Case 3 formula = %.6Lf \n",formula);
printf("==============================\n");
break;
}
}
printf("=======End of Switch Case=======\n");
return 0;
}
In the second case, the exact value is: 5.46682616404349358590563748207701185476524116704086448914383... × 10^19
Your code returned 5.4668261640434936064 x 10^19, so the error is quite small.
In the third case the exact value is: 2.535301200456458802993406410751 × 10^26. Your code returned 2.53530120045645892448944128 × 10^26, so there is a small error again.
So your code returns big numbers because result is supposed to be a big number.