开发者

Causes for NaN in C++ application that do no raise a floating point exception

开发者 https://www.devze.com 2023-02-18 05:44 出处:网络
To find the cause of floating point variables beeing set to NaN in my C++ program I enabled floating point exceptions like this:

To find the cause of floating point variables beeing set to NaN in my C++ program I enabled floating point exceptions like this:

#include <fenv.h>
fee开发者_JAVA技巧nableexcept(FE_INVALID | FE_OVERFLOW);

I know it works because when I write:

int val = 0.0/0.0;

in my program a floating point exception is risen. But NaNs are "spreading" through the floating point calculations of my program and I have no idea which variable is set to NaN first.

What causes for a variable beeing set to NaN exist, that would not cause a floating point exception?


If any input has a quiet NaN value, it will cause the result of nearly any floating-point operation that consumes it to be NaN without raising the invalid exception.

The most likely explanation is that some data is being read from the wrong address, and that the read data (which may not even be floating-point data) happens to match a quiet NaN encoding. This can happen pretty easily, because the relatively common pattern 0xffff... encodes a quiet NaN.


Are you doing any square root operation? If you try to compute a square root for a negative number as in sqrt(-1) then you will get a Nan. In this case you should get an 'Invalid Operation' exception though. Are you sure you are trapping correctly all exceptions? It seems like a numeric exception is untrapped somewhere in your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号