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 :(adsbygoogle = window.adsbygoogle || []).push({});

Is it because the i squared is too big for the int type ?Code (Text):#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);

}

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Multiplying small numbers and getting Inf

Loading...

Similar Threads - Multiplying small numbers | Date |
---|---|

Multiplying Big Numbers Using FFT | Oct 17, 2017 |

Matrix Chain Multiplication: Optimal way of multiplying | Feb 26, 2017 |

Python Adding very small values to very large numbers | Jun 9, 2016 |

Fortran Modern variable precision and integer multipliers | Mar 16, 2016 |

Multiplying two data sets which don't have the same length or spacing | Jul 23, 2012 |

**Physics Forums - The Fusion of Science and Community**