Questions tagged [fenv]
15 questions
22
votes
2 answers
Adding two floating-point numbers
I would like to compute the sum, rounded up, of two IEEE 754 binary64 numbers. To that end I wrote the C99 program below:
#include
#include
#pragma STDC FENV_ACCESS ON
int main(int c, char *v[]){
fesetround(FE_UPWARD);
…

Pascal Cuoq
- 79,187
- 7
- 161
- 281
16
votes
1 answer
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…

R.. GitHub STOP HELPING ICE
- 208,859
- 35
- 376
- 711
7
votes
1 answer
Does C standard's FE_TONEAREST rounding mode guarantee that halfway ties are rounded to even?
I am writing code that depends on halfway ties in C (specifically c11) rounding to even. When using rint with rounding mode as FE_TONEAREST, I have not found a guarantee in the C standard that states how ties are handled with FE_NEAREST.
Page 509 of…

Benjie
- 113
- 10
5
votes
5 answers
Floating point exceptions - gcc bug?
Consider the following code:
#include
#include
int main()
{
#pragma STDC FENV_ACCESS ON
1.0/0.0;
printf("%x\n", fetestexcept(FE_ALL_EXCEPT));
}
I would expect it to print a nonzero value corresponding to FE_DIVBYZERO,…

R.. GitHub STOP HELPING ICE
- 208,859
- 35
- 376
- 711
4
votes
1 answer
Why is fetestexcept in C++ compiled to a function call rather than inlined
I am evaluating the usage (clearing and querying) of Floating-Point Exceptions in performance-critical/"hot" code. Looking at the binary produced I noticed that neither GCC nor Clang expand the call to an inline sequence of instructions that I would…

Ikaros
- 395
- 1
- 10
3
votes
2 answers
C++ setting floating point exception environment
I'm struggling trying to set the std::fenv in a portable way.
Based on this cppreference page, it seems that fesetexceptflag(const std::fexcept_t*,int) should help me do the trick. On the other hand, I found out that GNU offers also the…

bartgol
- 1,703
- 2
- 20
- 30
3
votes
1 answer
Does FENV_ACCESS pragma exist in C++11 and higher?
Reading a bug report for clang not supporting FENV_ACCESS pragma I've come across a comment:
Setting the rounding mode without using #pragma STDC FENV_ACCESS ON invokes undefined behavior. See C11 7.6.1/2. (This pragma does not exist in C++, so…

Ruslan
- 18,162
- 8
- 67
- 136
2
votes
1 answer
C99 fenv.h for MS compilers
Anyone know of a decent C99 fenv.h implementation for MS compilers?
I imagine this wouldn't be difficult to do; I'm just looking to save some time.

defube
- 2,395
- 1
- 22
- 34
2
votes
1 answer
Can feenableexcept hurt a program performance?
I would like to enable floating point exceptions on some critical code using fpeenableexcept or _MM_SET_EXCEPTION_MASK (on Mac OS X).
Is there any case on x86/x86-64 where changing the enabled exceptions will hurt the program performance when no…

Thomas Moulard
- 5,151
- 2
- 21
- 28
1
vote
0 answers
feraiseexcept: different behavior between compilers and lack of documentation for implementation-defined behavior
Sample code (t91.c):
#include
#include
#if _MSC_VER
#pragma fenv_access (on)
#else
#pragma STDC FENV_ACCESS ON
#endif
void show_fe_exceptions(void)
{
printf("exceptions raised:");
if(fetestexcept(FE_DIVBYZERO)) printf("…

pmor
- 5,392
- 4
- 17
- 36
1
vote
1 answer
How are traps generated for floating point exceptions?
I want to know which code and files in the glibc library are responsible for generating traps for floating point exceptions when traps are enabled.
Currently, GCC for RISC-V does not trap floating point exceptions. I am interested in adding this…

FrackeR011
- 61
- 10
1
vote
1 answer
fegetenv() clears exception mask on x86_64-linux
Let's take the following program:
#include
#include
int main (void)
{
fenv_t e;
printf ("%d\n", fegetexcept () & FE_INVALID ? 1 : 0);
feenableexcept (FE_INVALID);
printf ("%d\n", fegetexcept () & FE_INVALID ? 1 : 0);
…

F'x
- 12,105
- 7
- 71
- 123
1
vote
0 answers
Why am I generating an undefined reference to _fe_dfl_env in 64-bit Cygwin
$ cat test.c
#include
#include
int main(int argc, char** argv) {
fesetenv(FE_DFL_ENV);
}
$ gcc -o a.exe test.c
/tmp/ccxbZr33.o:test.c:(.rdata$.refptr._fe_dfl_env[.refptr._fe_dfl_env]+0x0): undefined reference to…

Aron Ahmadia
- 2,267
- 2
- 19
- 22
0
votes
0 answers
How to raise different floating point exceptions in C?
I am interested in raising floating point exceptions like Division by zero, Overflow, Underflow, etc.
I think what i am trying to do is possible if we can change the default behavior of trap when floating point exception occurs.
I am using functions…

FrackeR011
- 61
- 10
0
votes
0 answers
Using fenv.h in MinGW
I want to change a floating-point rounding mode using standard library as suggested in this topic and cppreference. I use MingGW as an envirement. CMake is used to build of the project. My code:
main.cpp:
#include
#include…

Konstantin Isupov
- 199
- 1
- 2
- 12