Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/fenv/feraiseexcept"

From cppreference.com
< cpp‎ | numeric‎ | fenv
m (fmt, headers sorted)
m (fmt)
 
Line 8: Line 8:
 
{{dcl end}}
 
{{dcl end}}
  
Attempts to raise all floating point exceptions listed in {{tt|excepts}} (a bitwise OR of the [[cpp/numeric/fenv/FE_exceptions|floating point exception macros]]). If one of the exceptions is {{lc|FE_OVERFLOW}} or {{c|FE_UNDERFLOW}}, this function may additionally raise {{lc|FE_INEXACT}}. The order in which the exceptions are raised is unspecified, except that {{lc|FE_OVERFLOW}} and {{c|FE_UNDERFLOW}} are always raised before {{lc|FE_INEXACT}}.
+
Attempts to raise all floating point exceptions listed in {{tt|excepts}} (a bitwise OR of the [[cpp/numeric/fenv/FE_exceptions|floating point exception macros]]). If one of the exceptions is {{lc|FE_OVERFLOW}} or {{lc|FE_UNDERFLOW}}, this function may additionally raise {{lc|FE_INEXACT}}. The order in which the exceptions are raised is unspecified, except that {{lc|FE_OVERFLOW}} and {{lc|FE_UNDERFLOW}} are always raised before {{lc|FE_INEXACT}}.
  
 
===Parameters===
 
===Parameters===
Line 24: Line 24:
 
#include <iostream>
 
#include <iostream>
  
#pragma STDC FENV_ACCESS ON
+
// #pragma STDC FENV_ACCESS ON
  
 
int main()
 
int main()
 
{
 
{
 
     std::feclearexcept(FE_ALL_EXCEPT);
 
     std::feclearexcept(FE_ALL_EXCEPT);
     int r = std::feraiseexcept(FE_UNDERFLOW {{!}} FE_DIVBYZERO);
+
     const int r = std::feraiseexcept(FE_UNDERFLOW {{!}} FE_DIVBYZERO);
 
     std::cout << "Raising divbyzero and underflow simultaneously "
 
     std::cout << "Raising divbyzero and underflow simultaneously "
 
               << (r ? "fails" : "succeeds") << " and results in\n";
 
               << (r ? "fails" : "succeeds") << " and results in\n";
     int e = std::fetestexcept(FE_ALL_EXCEPT);
+
 
 +
     const int e = std::fetestexcept(FE_ALL_EXCEPT);
 
     if (e & FE_DIVBYZERO)
 
     if (e & FE_DIVBYZERO)
 
         std::cout << "division by zero\n";
 
         std::cout << "division by zero\n";

Latest revision as of 11:42, 25 April 2023

 
 
 
Floating-point environment
Functions
feraiseexcept
(C++11)
(C++11)(C++11)
(C++11)(C++11)
Macro constants
(C++11)
 
Defined in header <cfenv>
int feraiseexcept( int excepts );
(since C++11)

Attempts to raise all floating point exceptions listed in excepts (a bitwise OR of the floating point exception macros). If one of the exceptions is FE_OVERFLOW or FE_UNDERFLOW, this function may additionally raise FE_INEXACT. The order in which the exceptions are raised is unspecified, except that FE_OVERFLOW and FE_UNDERFLOW are always raised before FE_INEXACT.

Contents

[edit] Parameters

excepts - bitmask listing the exception flags to raise

[edit] Return value

0 if all listed exceptions were raised, non-zero value otherwise.

[edit] Example

#include <cfenv>
#include <iostream>
 
// #pragma STDC FENV_ACCESS ON
 
int main()
{
    std::feclearexcept(FE_ALL_EXCEPT);
    const int r = std::feraiseexcept(FE_UNDERFLOW | FE_DIVBYZERO);
    std::cout << "Raising divbyzero and underflow simultaneously "
              << (r ? "fails" : "succeeds") << " and results in\n";
 
    const int e = std::fetestexcept(FE_ALL_EXCEPT);
    if (e & FE_DIVBYZERO)
        std::cout << "division by zero\n";
    if (e & FE_INEXACT)
        std::cout << "inexact\n";
    if (e & FE_INVALID)
        std::cout << "invalid\n";
    if (e & FE_UNDERFLOW)
        std::cout << "underflow\n";
    if (e & FE_OVERFLOW)
        std::cout << "overflow\n";
}

Output:

Raising divbyzero and underflow simultaneously succeeds and results in
division by zero
underflow

[edit] See also

clears the specified floating-point status flags
(function) [edit]
determines which of the specified floating-point status flags are set
(function) [edit]
C documentation for feraiseexcept