Floating point exception in C code!

#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 does the output have infinite values ​​and NAN values?

+4
source share
8 answers

Others have indicated that you have problems with nan / inf, which is true, but here's how to fix your code to give you the results that I think you're looking for.

Since no one else has indicated this (as I noticed), you are trying to solve a system of differential equations using the method

+4
source

Your calculation is blown up. Just look at the values ​​printed for x and y, and you will see that they start to get very large, and then turn the inf information. Since your conditional is wrong, you end up using inf in a calculation that turns into nan.

+8
source

Comparison with inf or nan is done using the isnan() and isinf() functions, not in this way.
%lf for double , not float .

And, for God's sake, fclose() your file! (The first X-lines are some significant numbers.)

+6
source

Perhaps the conditional operator should be if (y == inf || y == nan)? y cannot be inf and NaN at the same time.

+3
source

Remember also that each comparison using NaN returns false regardless of which operator you use (<, <=, etc.) and what you are comparing it with.

+3
source

Since the break never happens, as indicated in the volume, and you never close the file, the data is not written, and then it all breaks when an exception actually occurs. Try clearing the data to a file in a for loop.

+1
source

I connected your equations to Excel and when t comes to 0.39 x and y, 2E + 270 and 5.2E + 270 are active. After that, they are too large to handle Excel. Here are some other meanings if you are interested (I know disgusting formatting):

  txy
 0.00 0 0
 0.01 9.9995E-05 0.0099995
 0.02 0.000719864 0.03099345
 0.03 0.002688085 0.065607372
 0.04 0.007431658 0.118589309
 0.05 0.017321769 0.197802239
 0.06 0.036281294 0.315992075
 0.07 0.070850729 0.493849065
 0.08 0.132072044 0.765266446
 0.09 0.238828626 1.186184238
 0.10 0.423641428 1.848128022
 0.11 0.741634646 2.89084744
 0.12 1.277397835 4.464693237
 0.13 2.107123319 6.382503724
 0.14 3.048840735 6.726552977
 0.15 3.361369798 2.083527082
 0.16 3.297988472 -0.396133288
 0.17 3.231095608 -0.393487431
 0.18 3.156811159 -0.412691383
 0.19 3.07386543 -0.436556472
 0.20 2.980485636 -0.466898968
 0.21 2.874086586 -0.506662146
 0.22 2.750700903 -0.56084401
 0.23 2.603841953 -0.638517177
 0.24 2.42203123 -0.757544679
 0.25 2.18283838 -0.9567714
 0.26 1.836632623 -1.331560601
 0.27 1.255566799 -2.152095647
 0.28 0.052253914 -4.297546016
 0.29 -2.923971917 -10.2628477
 0.30 -2.375067218 1.82968233
 0.31 -1.600801299 2.497631996
 0.32 0.082957072 5.261744912
 0.33 4.774399642 14.21649263
 0.34 -20.07952886 -73.09978971
 0.35 3522.569432 10121.85417
 0.36 -16277355468 -45214886085
 0.37 1.64003E + 30 4.43252E + 30
 0.38 -1.72156E + 90 -4.53043E + 90
 0.39 2.0423E + 270 5.2366E + 270
 0.40 #NUM!  #NUM!

So, I think the question is, should it compute this code?

0
source

Among other issues that have been addressed by others, a conditional break will never be triggered. y == nan false for each y , including NaN , so the condition is (y == inf && false) , which of course is false .

If you want to break when y is inf or NaN , you should use:

 if (isinf(y) || isnan(y)) break; 

Or if you want to use comparisons:

 if (fabs(y) == inf || y != y) break; 

(The fabs is on because you supposedly also want to break if y is -inf .)

0
source

Source: https://habr.com/ru/post/1308039/


All Articles