- #1
jk22
- 729
- 24
At approx. step 46000 the following code puts "Inf" out, but I can't understand why since it multiplies by itself a value smaller than 1. The sin is ok, but the loop for the power fails :
Is it because the i squared is too big for the int type ?
Code:
#include<stdio.h>
#include<math.h>
double power(double x, int n)
{
double result=1.0;
for(int i=0;i<n;i++)
result*=x;
return(result);
}
int main(void)
{
int nsub=800;
int iter=50000;
double sum=0.0;
int *distr=new int[nsub];
for(int i=1;i<iter;i++)
{
double val=pow(sin((double)i),i*i);
int subn=(int)(val*(double)nsub/2.0);
distr[subn+nsub/2]++;
if(fabs(val)>1.0) printf("%d %lf %lf\n",i,sin((double)i),val);
sum+=val;
}
printf("%lf\n",sum);
FILE *seriesout=fopen("series.out","w");
for(int i=0;i<nsub;i++)
fprintf(seriesout,"%lf %lf\n",2*(double)i/(double)nsub-1.0,(double)distr[i]/(double)iter);
}
Is it because the i squared is too big for the int type ?
Last edited by a moderator: