Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      MATH_ERRNO, MATH_ERREXCEPT, math_errhandling

      From cppreference.com
      <c‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Functions
      Basic operations
      (C99)
      (C99)
      (C99)
      (C99)(C99)(C99)(C23)
      Maximum/minimum operations
      (C99)
      (C99)
      Exponential functions
      (C23)
      (C99)
      (C99)
      (C23)
      (C23)

      (C99)
      (C99)(C23)
      (C23)
      (C23)
      Power functions
      (C99)
      (C23)
      (C23)

      (C99)
      (C23)
      (C23)
      Trigonometric and hyperbolic functions
      (C23)
      (C23)
      (C23)
      (C23)
      (C99)
      (C99)
      (C99)
      Nearest integer floating-point
      (C99)(C99)(C99)
      (C23)(C23)(C23)(C23)
      Floating-point manipulation
      (C99)(C99)
      (C99)(C23)
      (C99)
      Narrowing operations
      (C23)
      (C23)
      (C23)
      (C23)
      (C23)
      (C23)
      Quantum and quantum exponent
      Decimal re-encoding functions
      Total order and payload functions
      Classification
      Error and gamma functions
      (C99)
      (C99)
      (C99)
      (C99)
      Types
      Macro constants
      Special floating-point values
      (C99)(C23)
      Arguments and return values
      Error handling
      MATH_ERRNOMATH_ERRNOEXCEPT
      (C99)(C99) 
      math_errhandling
      (C99)

      Fast operation indicators
       
      Defined in header<math.h>
      #define MATH_ERRNO        1
      (since C99)
      #define MATH_ERREXCEPT    2
      (since C99)
      #define math_errhandling  /*implementation defined*/
      (since C99)

      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<fenv.h>.
      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_INVALIDacos(2)
      Pole errorthe mathematical result of the function is exactly infinite or undefinedERANGEFE_DIVBYZEROlog(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_OVERFLOWpow(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)sqrt(2),1.0/10.0

      Contents

      [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.rint vsnearbyint).

      Before C99, 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 <stdio.h>#include <fenv.h>#include <math.h>#include <errno.h>#pragma STDC FENV_ACCESS ONint main(void){printf("MATH_ERRNO is %s\n", math_errhandling& MATH_ERRNO?"set":"not set");printf("MATH_ERREXCEPT is %s\n",           math_errhandling& MATH_ERREXCEPT?"set":"not set");feclearexcept(FE_ALL_EXCEPT);errno=0;printf("log(0) = %f\n",log(0));if(errno==ERANGE)perror("errno == ERANGE");if(fetestexcept(FE_DIVBYZERO))puts("FE_DIVBYZERO (pole error) reported");}

      Possible output:

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

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 7.12/9 MATH_ERRNO, MATH_ERREXCEPT, math_errhandling (p: 170)
      • F.10/4 MATH_ERREXCEPT, math_errhandling (p: 377)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.12/9 MATH_ERRNO, MATH_ERREXCEPT, math_errhandling (p: 233)
      • F.10/4 MATH_ERREXCEPT, math_errhandling (p: 517)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.12/9 MATH_ERRNO, MATH_ERREXCEPT, math_errhandling (p: 214)
      • F.9/4 MATH_ERREXCEPT, math_errhandling> (p: 454)

      [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=c/numeric/math/math_errhandling&oldid=140217"

      [8]ページ先頭

      ©2009-2025 Movatter.jp