Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

floating point exception in C code !

Tags:

c

math

#include<stdio.h>
#include<math.h>

int main ()
{
    FILE *fp;
    fp=fopen("output","w");
    float t,y=0,x=0,e=5,f=1,w=1;
    for (t=0;t<10;t=t+0.01)
    {
        if( y==inf && y== nan) 
            break;
        fprintf(fp,"%lf\t%lf\n",y,x);
        y = y + ((e*(1 - x*x)*y) - x + f*cos(w*t))*t;
        x = x + y*t;
    }
    return 0;
}

Why is the ouput giving infinite and NAN values?

like image 404
Hick Avatar asked Nov 28 '22 04:11

Hick


1 Answers

Your calculation is blowing up. Just look at the values printed out for x and y and you will see they start to get very large and then turn info inf. Because your conditional is wrong, you wind up using inf in the calculation which turns into nan.

like image 116
frankc Avatar answered Dec 15 '22 05:12

frankc