Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get two different summations but whenever I run it I get the error 'Floating point exception (core dumped)'. I can't figure out why though. I've tried playing around with setting my n's to different values but that doesnt seem to change anything

What I have tried:

C
#include<stdio.h>  

int main()                                                                     
{   
   float s=0,t=0;
   int n;
   for(n=1;n<=123456;n++)
   {
        s=s+(2/(n*n));
   }
   n=0;
    for(n=123456;n>=1;n--)
   {
        t=t+(2/(n*n));
   }
   printf("\n%f",(3.14*3.14/3));  
   printf("\n%f",s);
   printf("\n%f",t);
   printf("\n%f",s-t);
   printf("\n%e",s-t);
   printf("\n%g",s-t);
   return(0);                                 
} 
Posted
Updated 16-Oct-20 4:39am
v2

1 solution

The exception is easily fixed by replacing
Quote:
s=s+(2/(n*n));
with
C
s=s+(2.0/(n*n));


and


Quote:
t=t+(2/(n*n));
with
t=t+(2.0/(n*n));



Note however, that obtaining a meaningful result using floats is more difficult.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900