Home > Back-end >  Operation results in 1 # INF00 because I double is not long enough, why use long double result becom
Operation results in 1 # INF00 because I double is not long enough, why use long double result becom

Time:09-27

 
#include
Void main ()
{
Int I, n, jiecheng=1;
Double s=0.0;
Printf (" please input the value of n ");
The scanf (" % d ", & amp; N);
for(i=1; i<=n; I++)
{
Jiecheng=jiecheng * I;
S=s + (1.0/jiecheng);
}
Printf (" results for % lf ", s);
}

CodePudding user response:

Int can only count to 12!

CodePudding user response:

Is probably the reason
Jiecheng=jiecheng * I
this seems to be a factorial scale growthLead to int jiecheng overflow the
Through tracking this value to 0,

This leads to 1.0/0, led to the incorrect, infinity
S=inf

CodePudding user response:

Estimated that has something to do with the compiler
When VS2015 c + + environment, long double is inf
 # include 
Int main ()
{
Int I, n, jiecheng=1;
Long double s=0.0 L;
Printf (" please input the value of n ");
The scanf (" % d ", & amp; N);
For (I=1; i <=n; I++)
{
Jiecheng=jiecheng * I;
S=s + (1.0/jiecheng);
}
Printf (" results for % lf ", s);
return 0;
}

CodePudding user response:

The problem is that jiecheng=jiecheng * I; When I=13 will have overflow
  • Related