Home C&C++函数库 c++ 语法 程序源码 Linux C库

math_errhandling

宏  <cmath> <tgmath.h>

int
错误处理
展开为一个表达式,该表达式标识<cmath>头文件中的函数所使用的错误处理机制:
常量 说明
MATH_ERRNO 1 errno用于表示错误:
-对于域错误:errno设置为EDOM。
-对于范围错误(极点错误,上溢出和下溢):errno设置为 ERANGE.
MATH_ERREXCEPT 2 C异常:
-域错误: FE_INVALID被触发。
-极点错误: FE_DIVBYZERO被触发。
-在溢出时: FE_OVERFLOW被触发。
-对于下溢出: FE_UNDERFLOW可能被触发。
MATH_ERRNO|MATH_ERREXCEPT 3 以上两种

MATH_ERRNO和MATH_ERREXCEPT都是宏常量表达式,在<cmath>中分别定义为1和2。

☣  示例



/* math_errhandling example */
#include <stdio.h>      /* printf */
#include <math.h>       /* math_errhandling */
#include <errno.h>      /* errno, EDOM */
#include <fenv.h>       /* feclearexcept, fetestexcept, FE_ALL_EXCEPT, FE_INVALID */
#pragma STDC FENV_ACCESS on

int main () {
  errno = 0;
  if (math_errhandling & MATH_ERREXCEPT) feclearexcept(FE_ALL_EXCEPT);

  printf ("Error handling: %d",math_errhandling);

  sqrt (-1);
  if (math_errhandling & MATH_ERRNO) {
    if (errno==EDOM) printf("errno set to EDOM\n");
  }
  if (math_errhandling  &MATH_ERREXCEPT) {
    if (fetestexcept(FE_INVALID)) printf("FE_INVALID raised\n");
  }

  return 0;
}

可能的输出:
Error handling: 3
errno set to EDOM
FE_INVALID raised

↭  数据竞争



支持多线程的库应该在每个线程的基础上实现errno和/或浮点异常状态: 每个线程都有自己的本地errno和浮点状态。

这是符合C11和c++ 11标准的库的要求。
errno 错误码(宏)

联系我们 免责声明 关于CandCplus 网站地图