困住安静的NaN [英] Trapping quiet NaN

查看:73
本文介绍了困住安静的NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中某些组件偶尔在大型数据流中插入一个qNaN,然后​​使整个处理无效(对包含单个qNaN的向量进行FFT会得到全qNaN的输出)。现在,我想在行为中捕获该组件,并找出其原因。

I have an application where some component occasionally inserts a qNaN in a large data stream, which then invalidates the entire processing (FFT on a vector containing a single qNaN results in an all-qNaN output). Now I'd like to catch that component in the act and find out why it's doing that.

为此,我需要以某种方式在调试过程中使所有NaN发出信号。有没有办法用x64 CPU执行32位代码?

For this, I'd need to somehow make all NaNs signalling during debug. Is there a way to do so, with a x64 CPU executing 32 bit code?

推荐答案

如果要制作所有NaN,

If you want to make all NaNs, overflows, and zerodivides signaling during debug, it is possible.

对于gcc:

#include <fenv.h>

#ifndef NDEBUG
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
#endif

对于Visual Studio(未测试):

For Visual Studio (not tested):

#include <float.h>

#ifndef NDEBUG
_clearfp();
_controlfp(_controlfp(0, 0) & ~(_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW),
           _MCW_EM);
#endif

参考文献: Microsoft gcc

这些功能允许捕获浮点运算(溢出,零除,无效运算)产生的任一NaN。或sNaN,用作某些浮点运算的输入。它们不允许捕获用作浮点操作输入的qNaN。对于此类qNaN,找到它们的唯一方法是单独检查每个值(请参见Luchian Grigore的答案)。

These functions allow to catch either NaNs, produced by floating point operations (overflows, zerodivides, invalid operations), or sNaNs, used as input to some floating point operation. They do not allow to catch qNaNs, used as input to floating point operation. For such qNaNs, the only way to find them is to check each value individually (see Luchian Grigore's answer).

因此,如果插入qNaN的组件位于同一程序,是否要捕获它,或者该组件在单独的程序中,但是您有其源代码,只需使用 feenableexcept() / <$启用FP例外c $ c> _controlfp()。否则,请使用 isnan()(C ++ 11)或 x!= x

So if the component, that inserts a qNaN is in the same program, were you want to catch it, or if this component is in separate program, but you have its source codes, just enable FP exceptions with feenableexcept()/_controlfp(). Otherwise, check every value in the incoming data stream with isnan() (C++11) or with x != x.

这篇关于困住安静的NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆