Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      MATH_ERRNO, MATH_ERREXCEPT, math_errhandling

      From cppreference.com
      <cpp‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Nearest integer floating point operations
      (C++11)(C++11)(C++11)
      (C++11)
      (C++11)
      (C++11)(C++11)(C++11)
      Floating point manipulation functions
      (C++11)(C++11)
      (C++11)
      (C++11)
      Classification and comparison
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Types
      (C++11)
      (C++11)
      (C++11)
      Macro constants
      math_errhandlingMATH_ERRNOMATH_ERREXCEPT
      (C++11)  
      (C++11)
      (C++11)
       
      Defined in header<cmath>
      #define MATH_ERRNO        1
      (since C++11)
      #define MATH_ERREXCEPT    2
      (since C++11)
      #define math_errhandling  /*implementation defined*/
      (since C++11)

      The macro constantmath_errhandling expands to an expression of typeint that is either equal toMATH_ERRNO, or equal toMATH_ERREXCEPT, or equal to their bitwise OR (MATH_ERRNO| MATH_ERREXCEPT).

      The value ofmath_errhandling indicates the type of error handling that is performed by the floating-point operators andfunctions:

      Constant Explanation
      MATH_ERREXCEPT Indicates that floating-point exceptions are used: at leastFE_DIVBYZERO,FE_INVALID, andFE_OVERFLOW are defined in<cfenv>.
      MATH_ERRNO Indicates that floating-point operations use the variableerrno to report errors.

      If the implementation supports IEEE floating-point arithmetic (IEC 60559),math_errhandling& MATH_ERREXCEPT is required to be non-zero.

      The following floating-point error conditions are recognized:

      ConditionExplanationerrnoFloating-point exceptionExample
      Domain errorThe argument is outside the range in which the operation is mathematically defined (the description ofeach function lists the required domain errors)EDOMFE_INVALIDstd::acos(2)
      Pole errorThe mathematical result of the function is exactly infinite or undefinedERANGEFE_DIVBYZEROstd::log(0.0),1.0/0.0
      Range error due to overflowThe mathematical result is finite, but becomes infinite after rounding, or becomes the largest representable finite value after rounding downERANGEFE_OVERFLOWstd::pow(DBL_MAX,2)
      Range error due to underflowThe result is non-zero, but becomes zero after rounding, or becomes subnormal with a loss of precisionERANGE or unchanged (implementation-defined)FE_UNDERFLOW or nothing (implementation-defined)DBL_TRUE_MIN/2
      Inexact resultThe result has to be rounded to fit in the destination typeUnchangedFE_INEXACT or nothing (unspecified)std::sqrt(2),1.0/10.0

      [edit]Notes

      WhetherFE_INEXACT is raised by the mathematical library functions is unspecified in general, but may be explicitly specified in the description of the function (e.g.std::rint vsstd::nearbyint).

      Before C++11, floating-point exceptions were not specified,EDOM was required for any domain error,ERANGE was required for overflows and implementation-defined for underflows.

      [edit]Example

      Run this code
      #include <cerrno>#include <cfenv>#include <cmath>#include <cstring>#include <iostream>// #pragma STDC FENV_ACCESS ON int main(){std::cout<<"MATH_ERRNO is "<<(math_errhandling& MATH_ERRNO?"set":"not set")<<'\n'<<"MATH_ERREXCEPT is "<<(math_errhandling& MATH_ERREXCEPT?"set":"not set")<<'\n';std::feclearexcept(FE_ALL_EXCEPT);errno=0;std::cout<<"log(0) = "<<std::log(0)<<'\n';if(errno==ERANGE)std::cout<<"errno = ERANGE ("<<std::strerror(errno)<<")\n";if(std::fetestexcept(FE_DIVBYZERO))std::cout<<"FE_DIVBYZERO (pole error) reported\n";}

      Possible output:

      MATH_ERRNO is setMATH_ERREXCEPT is setlog(0) = -inferrno = ERANGE (Numerical result out of range)FE_DIVBYZERO (pole error) reported

      [edit]See also

      floating-point exceptions
      (macro constant)[edit]
      macro which expands to POSIX-compatible thread-local error number variable
      (macro variable)[edit]
      C documentation formath_errhandling
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/math_errhandling&oldid=160773"

      [8]ページ先頭

      ©2009-2025 Movatter.jp