Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of feholdexcept etc.?

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?

like image 853
R.. GitHub STOP HELPING ICE Avatar asked Jun 12 '11 13:06

R.. GitHub STOP HELPING ICE


1 Answers

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.

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

like image 194
Stephen Canon Avatar answered Nov 16 '22 00:11

Stephen Canon