MATH_ERRNO, MATH_ERREXCEPT, math_errhandling
| Common mathematical functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical special functions (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical constants (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic linear algebra algorithms (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data-parallel types (SIMD) (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Floating-point environment (C++11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Numeric array ( 
                valarray
               ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bit manipulation (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Saturation arithmetic (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | 
 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | 
 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | 
 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Nearest integer floating point operations | |||||||||||||||||||||||||||||||||||||||||
| 
 | 
 | ||||||||||||||||||||||||||||||||||||||||
| Floating point manipulation functions | |||||||||||||||||||||||||||||||||||||||||
| 
 | 
 | ||||||||||||||||||||||||||||||||||||||||
| Classification and comparison | |||||||||||||||||||||||||||||||||||||||||
| 
 | |||||||||||||||||||||||||||||||||||||||||
| Types | |||||||||||||||||||||||||||||||||||||||||
| Macro constants | |||||||||||||||||||||||||||||||||||||||||
| 
 | 
 | ||||||||||||||||||||||||||||||||||||||||
| 
           定义于头文件
            
            
             <cmath>
            
            | ||
| 
           
            
             #define MATH_ERRNO        1
            
           
           | (C++11 起) | |
| 
           
            
             #define MATH_ERREXCEPT    2
            
           
           | (C++11 起) | |
| 
           
            
             #define math_errhandling  /*由实现定义*/
            
           
           | (C++11 起) | |
       宏常量
       
        math_errhandling
       
       展开为一个类型为
       
        
         int
        
       
       的表达式,其值可能等于
       
        MATH_ERRNO
       
       ,或等于
       
        MATH_ERREXCEPT
       
       ,或等于二者的按位或值(
       
        
         MATH_ERRNO
         
          |
         
         MATH_ERREXCEPT
        
       
       )。
      
       
        math_errhandling
       
       的值指示浮点运算符及
       
        函数
       
       所执行的错误处理类型:
      
| 常量 | 说明 | 
| 
          MATH_ERREXCEPT
          | 表示使用浮点异常:至少 FE_DIVBYZERO 、 FE_INVALID 和 FE_OVERFLOW 在 <cfenv> 中定义。 | 
| 
          MATH_ERRNO
          | 表示浮点运算使用变量 errno 报告错误。 | 
如果实现支持 IEEE 浮点算术(IEC 60559),则要求 math_errhandling & MATH_ERREXCEPT 必须为非零值。
以下浮点错误条件被识别:
| 条件 | 说明 | errno | 浮点异常 | 示例 | 
|---|---|---|---|---|
| 定义域错误 | 参数超出数学运算定义的取值范围( 各函数 说明中列出了所需的定义域错误) | EDOM | FE_INVALID | std:: acos ( 2 ) | 
| 极点错误 | 函数的数学结果恰好为无穷大或未定义 | ERANGE | FE_DIVBYZERO | std:: log ( 0.0 ) , 1.0 / 0.0 | 
| 由于上溢导致的取值范围错误 | 数学结果是有限的,但经舍入后变为无穷大,或经向下舍入后变为可表示的最大有限值 | ERANGE | FE_OVERFLOW | std:: pow ( DBL_MAX , 2 ) | 
| 由于下溢导致的取值范围错误 | 结果非零,但经舍入后变为零,或因精度损失变为次正规数 | ERANGE 或保持不变(由实现定义) | FE_UNDERFLOW 或无异常(由实现定义) | DBL_TRUE_MIN / 2 | 
| 不精确结果 | 结果需经舍入才能匹配目标类型 | 保持不变 | FE_INEXACT 或无异常(未指定) | std:: sqrt ( 2 ) , 1.0 / 10.0 | 
注释
数学库函数是否会引发 FE_INEXACT 通常未作规定,但可能在函数描述中明确说明(例如 std::rint 与 std::nearbyint 的对比)。
在C++11之前,浮点异常未被规范定义, EDOM 用于所有定义域错误, ERANGE 用于处理溢出情况,而下溢则由实现定义处理方式。
示例
#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"; }
可能的输出:
MATH_ERRNO is set MATH_ERREXCEPT is set log(0) = -inf errno = ERANGE (Numerical result out of range) FE_DIVBYZERO (pole error) reported
参见
| 浮点异常 (宏常量) | |
| 扩展为POSIX兼容的线程局部错误编号变量的宏 (宏变量) | |
| 
          
           
            C 文档
           
          
          关于
          
           
            math_errhandling
           
          
          | |