printf not working with long type (resolved)
Code:
long stirling1(int n)
{
double nd = n;
return ( pow(nd,nd) * exp(-nd) * sqrt( (2 * PI * nd) ));
}
Code:
long stirling2(int n)
{
double nd = n;
return ( pow(nd,nd) * exp(-nd) * sqrt( (2 * PI * nd) ) * (1 + 1/(12*nd)) );
}
Code:
for(int i = 1; i<=input; i++)
printf("%4d %14d %20.10e %20.10e\n", i, factorial(i), stirling1(i), stirling2(i));
The above code does not print the correct values for stirling1() and stirling2(). I think this has something to do with the printf because when i change the "%20.10e" to "%20d" the numbers are correct in decimal form. I know the calculations are right, but i can't figure out why the printing is wrong.
Here is the output:
Code:
Number Factorial Approximation1 Approximation2
---------------------------------------------------------------------
1 1 0.0000000000e+00 4.2419181287e-314
2 2 2.1219957915e-314 6.3639139197e-314
3 6 1.0609978957e-313 1.4851897084e-313
4 24 4.8805903204e-313 5.3047821321e-313
5 120 2.5039550339e-312 2.5675941725e-312
6 720 1.5066170119e-311 1.5299568918e-311
7 5040 1.0567539041e-310 1.0696978709e-310
8 40320 8.4671876071e-310 8.5560990214e-310
9 362880 7.6293387888e-309 7.7003195255e-309
10 3628800 1.2745071711e-307 1.3000610917e-307
11 39916800 2.7223431127e-297 3.2649323566e-297