[英]Causes for NaN in C++ application that do no raise a floating point exception
To find the cause of floating point variables beeing set to NaN in my C++ program I enabled floating point exceptions like this: 为了找到在我的C ++程序中设置为NaN的浮点变量的原因,我启用了这样的浮点异常:
#include <fenv.h>
feenableexcept(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.
但NaNs正在通过我的程序的浮点计算“传播”,我不知道哪个变量首先设置为NaN。
What causes for a variable beeing set to NaN exist, that would not cause a floating point exception? 存在导致变量设置为NaN的原因是什么,这不会导致浮点异常?
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 . 如果任何输入具有安静的NaN值,则它将导致几乎任何使用它的浮点运算的结果为NaN 而不引发无效异常 。
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. 最可能的解释是,某些数据正在从错误的地址读取,并且读取的数据(甚至可能不是浮点数据)恰好与安静的NaN编码相匹配。 This can happen pretty easily, because the relatively common pattern
0xffff...
encodes a quiet NaN. 这很容易发生,因为相对常见的模式
0xffff...
编码一个安静的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.
如果您尝试计算负数的平方根,如sqrt(-1)那么您将得到一个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.
似乎数字异常在代码中的某处未被包装。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.