什么语言向开发者公开IEEE 754陷阱? [英] what languages expose IEEE 754 traps to the developer?

查看:164
本文介绍了什么语言向开发者公开IEEE 754陷阱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



数值计算中的默认行为的一个常见问题是我们错过了Nan(或+ -inf)出现在一个错误的操作。默认行为是通过计算进行传播,但是一些操作(比如比较)打破了链,并且松开了Nan,剩下的处理继续进行而没有承认在算法的前面的步骤中的奇点。

有时我们有办法对这种事件作出反应:延长一个函数(在我的例子中为0/0 = 12),或者在时域模拟中抛出一步,尝试其他设置如预测器,步长或其他)。

所以,这里是我的问题:你知道向开发者公开IEEE754陷阱的语言吗?我不喜欢用ASM搞这个。

解决方案

据我所知,你有两个选择浮点在C和C ++中的异常处理:首先,如果你禁用/屏蔽浮点异常(大多数环境默认是这样),你可以看到是否有任何浮点异常通过调用 fetestexcept 发生。 fetestexcept在Visual C ++中不可用,但您可以轻松窃取MinGW Runtime的实现。 (在公共领域)一旦异常被标记出来,直到你调用feclearexcept才会被清除,所以你可以在一系列计算结束时调用fetestexcept,看看是否有任何异常。这不会给你所要求的陷阱,但它确实可以让你测试NaN或+/- inf等问题是否已经发生,并根据需要作出反应。



其次,您可以通过在Linux或 feenableexcept 来启用/取消屏蔽浮点异常href =http://msdn.microsoft.com/en-us/library/e9b52ceh(VS.80).aspx =nofollow noreferrer> _ controlfp 。在操作系统中处理由处理器生成的浮点异常取决于您的操作系统。




  • 在Linux中,操作系统发送一个SIGFPE信号,所以你可以安装一个信号处理程序来捕捉它,并设置一个标志,告诉你的例程做出适当的反应。
  • 在Windows中,操作系统调用结构化异常处理来将处理器异常转换成语言你可以在C中使用 __ try / __ catch 块来捕获异常,或者使用 try / catch block。
  • 这个答案,你应该能够使用 _MM_SET_EXCEPTION_MASK xmmintrin.h 启用/取消屏蔽异常,只要您使用默认编译器选项(即,不要禁用SSE),你应该能够赶上前使用SIGFPE的选择。


(我已经在C和C ++的这个博客文章,如果你好奇的话。)


I'd like to play with those traps for educational purpose.

A common problem with the default behavior in numerical calculus is that we "miss" the Nan (or +-inf) that appeared in a wrong operation. Default behavior is propagation through the computation, but some operation (like comparisons) break the chain and loose the Nan, and the rest of the treatment continue without acknowledging the singularity in previous steps of the algorithm.

Sometimes we have ways to react to this kind of event : prolongating a function ("0/0 = 12 in my case"), or in time-domain simulation throwing the step away and trying with other settings (like the predictor, the step size or whatever).

So here is my question : do you know languages that expose the IEEE754 traps to the developer ? I don't feel like messing with ASM for that.

解决方案

As far as I know, you have two choices for floating point exception handling in C and C++:

First, if you disable/mask floating point exceptions (which most environments do by default), you can see whether any floating point exceptions have occurred by calling fetestexcept. fetestexcept isn't available in Visual C++, but you can steal the MinGW Runtime's implementation easily enough. (It's in the public domain.) Once an exception has been flagged, it's not cleared until you call feclearexcept, so you can call fetestexcept at the end of a series of calculations to see if any of them raised an exception. This doesn't give you the traps that you asked for, but it does let you test if problems like NaN or +/-inf have occurred and react as needed.

Second, you can enable/unmask floating point exceptions by calling feenableexcept in Linux or _controlfp in Windows. How the operating system handles a processor-generated floating point exception depends on your operating system.

  • In Linux, the OS sends a SIGFPE signal, so you can install a signal handler to catch that and set a flag that tells your routine to react appropriately.
  • In Windows, the OS invokes Structured Exception Handling to convert the processor exception into a language exception that you can catch using a __try / __catch block in C or try / catch block in C++.
  • Update: For Mac OS X, as described in this answer, you should be able to enable/unmask exceptions using _MM_SET_EXCEPTION_MASK from xmmintrin.h, and as long as you use the default compiler options (i.e., don't disable SSE), you should be able to catch exceptions using SIGFPE.

(I've written a bit more on this and other floating point issues in C and C++ in this blog posting if you're curious.)

这篇关于什么语言向开发者公开IEEE 754陷阱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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