如何使用 std::signaling_nan?
在查看了关于 SO 的另一个问题(Using NaN in C++)后,我对 感到好奇std::numeric_limits
。
我无法让signaling_NaN 抛出异常。 我想也许通过发信号它确实意味着一个信号,所以我尝试捕获 SIGFPE 但没有...
这是我的代码:
double my_nan = numeric_limits<double>::signaling_NaN();
my_nan++;
my_nan += 5;
my_nan = my_nan / 10;
my_nan = 15 / my_nan;
cout << my_nan << endl;
numeric_limits
计算结果为 true,因此它在我的系统上实现。
有任何想法吗?
我正在使用 ms Visual Studio .net 2003 的 C++ 编译器。 回家后我想在另一个身上测试一下。
谢谢!
After looking at another question on SO (Using NaN in C++) I became curious about std::numeric_limits<double>::signaling_NaN()
.
I could not get signaling_NaN to throw an exception. I thought perhaps by signaling it really meant a signal so I tried catching SIGFPE but nope...
Here is my code:
double my_nan = numeric_limits<double>::signaling_NaN();
my_nan++;
my_nan += 5;
my_nan = my_nan / 10;
my_nan = 15 / my_nan;
cout << my_nan << endl;
numeric_limits<double>::has_signaling_NaN
evaluates to true, so it is implemented on my system.
Any ideas?
I am using ms visual studio .net 2003's C++ compiler. I want to test it on another when I get home.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
_control87()
函数启用浮点异常。 来自_control87()
的 MSDN 文档:启用浮点异常后,您可以使用
signal()
或 SEH(结构化异常处理) 抓住他们。You can use the
_control87()
function to enable floating-point exceptions. From the MSDN documentation on_control87()
:When floating point exceptions are enabled, you can use
signal()
or SEH (Structured Exception Handling) to catch them.一句警告:
使用第 3 方 DLL 可能会默默地启用这些异常。 对于加载用默认启用它们的语言编写的 DLL 来说尤其如此。
我在两个实例中发生过这种情况:从嵌入式浏览器控件打印到 HP 打印机,以及从用 Delphi 编写的 InnoSetup 注册我的 DLL(将一些初始值设置为 NaN)。
A word of warning:
Using 3rd party DLLs may silently enable these exceptions. This is especially true for loading DLL's that are written in a language that enables them by default.
I've had that happen in two instances: Printing from an embedded browser control to a HP printer, and registering my DLL (that sets some initial values to NaN) from InnoSetup which is written in Delphi.
来自TFM:
->
其中“Q”代表“Quiet”。 不知道为什么它会返回它,但这就是为什么它不会为你抛出异常。
出于好奇,这样效果更好吗?
From TFM:
->
where the 'Q' stands for 'Quiet'. Dunno why it would return that, but that's why it doesn't throw an exception for you.
Out of curiosity, does this work better?
关键在于
numeric_limits: :has_signaling_NaN
。 这对你来说有什么价值? (MSDN 似乎表明对于 MSVC 来说它总是false
?)The key lies in
numeric_limits<T>::has_signaling_NaN
. Which value does this have for you? (The MSDN seems to suggest that it's alwaysfalse
for MSVC?)