feholdexcept等有什么用?

时间:2022-04-11 20:23:06

The documentation (in the standards) for all of fenv.h is rather confusing, but I'm especially confused about feholdexcept and the concept of "non-stop mode" for a floating point exception. As far as I can tell, on any IEEE floating point implementation, exceptions are non-signaling/"non-stop" by default, and the fenv.h interfaces seem to provide no way to enable a signaling mode unless it was the default. Is the whole concept of feholdexcept useless except on non-IEEE systems or systems with nonstandard extensions for setting the signaling exception mask?

所有fenv.h的文档(在标准中)都相当混乱,但我对feholdexcept和浮点异常的“不停止模式”的概念特别困惑。据我所知,在任何IEEE浮点实现中,默认情况下异常都是非信令/“不停止”,并且fenv.h接口似乎无法启用信令模式,除非它是默认模式。除非在非IEEE系统或具有非标准扩展的系统上设置信令异常掩码,feholdexcept的整个概念是否无用?

1 个解决方案

#1


6  

Suppose that you're implementing a library, and you don't know anything about what your callers might do the the floating-point environment before calling your code. They might unmask an exception, and install a custom trap handler that causes division-by-zero to produce the value 42. Suppose that your library depends on having default IEEE-754 behavior for division-by-zero. The feholdexcept function gives you a means to enforce this behavior. The caller's environment, complete with their unmasked exception, can then be restored using the fesetenv function.

假设您正在实现一个库,并且在调用代码之前,您不知道调用者可能在浮点环境中执行的操作。它们可能会取消屏蔽异常,并安装一个自定义陷阱处理程序,该处理程序会导致除零以产生值42.假设您的库依赖于具有除零的默认IEEE-754行为。 feholdexcept函数为您提供了强制执行此行为的方法。然后可以使用fesetenv函数恢复调用者的环境以及未屏蔽的异常。

This is admittedly a fairly obscure corner case of usage, but frankly everything in fenv.h is fairly obscure to most programmers.

这无疑是一个相当模糊的使用角落,但坦率地说,对于大多数程序员来说,fenv.h中的所有内容都相当模糊。

#1


6  

Suppose that you're implementing a library, and you don't know anything about what your callers might do the the floating-point environment before calling your code. They might unmask an exception, and install a custom trap handler that causes division-by-zero to produce the value 42. Suppose that your library depends on having default IEEE-754 behavior for division-by-zero. The feholdexcept function gives you a means to enforce this behavior. The caller's environment, complete with their unmasked exception, can then be restored using the fesetenv function.

假设您正在实现一个库,并且在调用代码之前,您不知道调用者可能在浮点环境中执行的操作。它们可能会取消屏蔽异常,并安装一个自定义陷阱处理程序,该处理程序会导致除零以产生值42.假设您的库依赖于具有除零的默认IEEE-754行为。 feholdexcept函数为您提供了强制执行此行为的方法。然后可以使用fesetenv函数恢复调用者的环境以及未屏蔽的异常。

This is admittedly a fairly obscure corner case of usage, but frankly everything in fenv.h is fairly obscure to most programmers.

这无疑是一个相当模糊的使用角落,但坦率地说,对于大多数程序员来说,fenv.h中的所有内容都相当模糊。