Using C++11:

I am trying to determine how to handle domain and range errors using the g++ compiler (Mingw64). I have provided some sample code below which is the approach that I use for g++ on Linux systems. When I compile I get the following messages:

warning: ignoring #pragma STDC FENV_ACCESS [-Wunknown-pragmas] #pragma STDC FENV_ACCESS ON

error: 'math_errhandling' was not declared in this scope

error: 'MATH_ERRNO' was not declared in this scope

error: 'MATH_ERREXCEPT' was not declared in this scope

I have looked for these symbols in the normal include headers but I don't see them. Any help is greatly appreciated. I must note that math_errhandling, MATH_ERRNO, MATH_ERREXCEPT and the pragma STDC FENV_ACCESS are part of the c++(2011) standard.

#include <cmath>

#include <fenv>

#include <cerrno>

#pragma STDC FENV_ACCESS ON

if (math_errhandling & MATH_ERREXCEPT) {

feclearexcept(FE_ALL_EXCEPT);

}

errno = 0;

result = pow(x, y);

if ((math_errhandling & MATH_ERRNO) && errno != 0) {

/* Handle range error */

}

else if ((math_errhandling & MATH_ERREXCEPT) &&

fetestexcept(FE_INVALID | FE_DIVBYZERO |

FE_OVERFLOW | FE_UNDERFLOW) != 0) {

/* Handle range error */

}

}

/ Use result... /

}

I am trying to determine how to handle domain and range errors using the g++ compiler (Mingw64). I have provided some sample code below which is the approach that I use for g++ on Linux systems. When I compile I get the following messages:

warning: ignoring #pragma STDC FENV_ACCESS [-Wunknown-pragmas] #pragma STDC FENV_ACCESS ON

error: 'math_errhandling' was not declared in this scope

error: 'MATH_ERRNO' was not declared in this scope

error: 'MATH_ERREXCEPT' was not declared in this scope

I have looked for these symbols in the normal include headers but I don't see them. Any help is greatly appreciated. I must note that math_errhandling, MATH_ERRNO, MATH_ERREXCEPT and the pragma STDC FENV_ACCESS are part of the c++(2011) standard.

#include <cmath>

#include <fenv>

#include <cerrno>

#pragma STDC FENV_ACCESS ON

if (math_errhandling & MATH_ERREXCEPT) {

feclearexcept(FE_ALL_EXCEP

}

errno = 0;

result = pow(x, y);

if ((math_errhandling & MATH_ERRNO) && errno != 0) {

/* Handle range error */

}

else if ((math_errhandling & MATH_ERREXCEPT) &&

fetestexcept(FE_INVALID | FE_DIVBYZERO |

FE_OVERFLOW | FE_UNDERFLOW) != 0) {

/* Handle range error */

}

}

/ Use result... /

}

I have looked for these symbols in the normal include headers but I don't see them.you have to look in your linux system where those expressions are coming from.

i have doubts that those non-standard expressions are portable to mingw. so i would try to comment all those things that don't compile or replace them by own functions (based on c++ standard modules only) if you think you would need them.

for example

```
if ((math_errhandling & MATH_ERRNO) && errno != 0)
```

might be replaced by

```
if (errno != 0)
```

probably without disadvantages because it was defeatable in the linux code as well.

Sara

int main ()

{

fpeInit_ fenv_t fe;

int fint;

feholdexcept(&fe);

feclearexcept (FE_ALL_EXCEPT);

int = fetestexcept(FE_ALL_EXCEPT

if (fint & FE_DIVBYZERO || fint & FE_INVALID || fint & FE_OVERFLOW || fint & FE_UNDERFLOW)

{

feclearexcept (FE_ALL_EXCEPT);

feupdateenv(&fe);

}

}

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

I see that you are using

#include <fenv>

I am not sure where you got that header file. For pure C++11, you should be using

#include <cfenv>

I have also seen <fenv.h> online.

Try running the code in this link

http://en.cppreference.com/w/cpp/numeric/math/math_errhandling

Make sure you include the compiler option to use C++11:

g++ -std=c++11

Open in new window