We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I accidentally stuff the value of 0xFFFF into a float variable (fVal). My emulator shows me its value is -1#nan (not a number) and my limit clipping code did not work on this value ie, if (fVal > fMax) fVal = fMax; else if (fVal < fMin) fVal = fMin; You'd think that the fVal would always be inside the limits of fMin and fMax for all values of fVal, but the code did not work when fVal contains the -1#nan value. How should I handle this? Is this a compiler bug? Thanks Andy
I believe there is a library function _chkfloat_ which I think may validate a floating point number, although I can't find any documentation for it in my manual. It appears in the include file "intrins.h". Not all combinations of the 32 bits in an IEEE floating point number represent valid floats, take a look at one of the many websites that have a discussion of IEEE floating point format. The best way to handle this is not to stuff values into floats! And no, it isn't a bug in the compiler. Stefan
although I can't find any documentation for it in my manual Page 243 in the Cx51 User's Guide. Jon
Thanks. I found out about _chkfloat_ and will use it from now on when appropriate. It's hard *not* to stuff things into floats when you're getting the float value from a different memory space like throught the serial port. Andy
"Page 243 in the Cx51 User's Guide." Now, wouldn't it be nice if it were in the index as well? Stefan
"It's hard *not* to stuff things into floats when you're getting the float value from a different memory space like throught the serial port." Good point. Stefan
How should I handle this? Probably not at all. ;-) The behaviour you got is exactly expected from a NaN value: all comparisons you make that involve a NaN, will return "false". An old trick to detect them thus always was:
if (!(fVal == fVal)) { /* Ouch! fVal is a NaN. Now what? */ }