zoukankan      html  css  js  c++  java
  • FE_INVALID (Numerics) – C 中文开发手册

    [
  •   C 语言中文开发手册

    FE_INVALID (Numerics) - C 中文开发手册

    在头文件<fenv.h>中定义
    #define FE_DIVBYZERO /*implementation defined power of 2*/ (自C99)
    #define FE_INEXACT /*implementation defined power of 2*/ (自 C99)
    #define FE_INVALID /*implementation defined power of 2*/ (自 C99)
    #define FE_OVERFLOW /*implementation defined power of 2*/ (自C99)
    #define FE_UNDERFLOW /*implementation defined power of 2*/ (自C99)
    #define FE_ALL_EXCEPT FE_DIVBYZERO | FE_INEXACT | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW (自 C99)

    所有这些宏常量(FE_ALL_EXCEPT除外)都展开为不同幂的2的整型常量表达式,它们唯一标识所有受支持的浮点异常。 每个宏只在被支持时才被定义。扩展为所有其他FE_ *的按位OR的宏常量FE_ALL_EXCEPT始终定义,如果实现不支持浮点异常,则该常量将为零。

    常量 说明
    FE_DIVBYZERO 极点错误发生在较早的浮点运算中
    FE_INEXACT 不精确的结果:舍入对于存储较早浮点运算的结果是必要的
    FE_INVALID 在早期的浮点操作中发生域错误
    FE_OVERFLOW 较早浮点运算的结果太大而无法表示
    FE_UNDERFLOW 早期浮点运算的结果是低于正常的,并且精度下降
    FE_ALL_EXCEPT 按位或所有受支持的浮点异常

    该实现可以在<fenv.h>中定义附加的宏常量来标识附加的浮点异常。 所有这些常量都以FE_开头,后跟至少一个大写字母。有关更多详细信息,请参阅math_errhandling。

    #include <stdio.h>
    #include <math.h>
    #include <float.h>
    #include <fenv.h>
     
    #pragma STDC FENV_ACCESS ON
    void show_fe_exceptions(void)
    {
        printf("exceptions raised:");
        if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO");
        if(fetestexcept(FE_INEXACT))   printf(" FE_INEXACT");
        if(fetestexcept(FE_INVALID))   printf(" FE_INVALID");
        if(fetestexcept(FE_OVERFLOW))  printf(" FE_OVERFLOW");
        if(fetestexcept(FE_UNDERFLOW)) printf(" FE_UNDERFLOW");
        feclearexcept(FE_ALL_EXCEPT);
        printf("
    ");
    }
     
    int main(void)
    {
        printf("MATH_ERREXCEPT is %s
    ",
               math_errhandling & MATH_ERREXCEPT ? "set" : "not set");
     
        printf("0.0/0.0 = %f
    ", 0.0/0.0);
        show_fe_exceptions();
     
        printf("1.0/0.0 = %f
    ", 1.0/0.0);
        show_fe_exceptions();
     
        printf("1.0/10.0 = %f
    ", 1.0/10.0);
        show_fe_exceptions();
     
        printf("sqrt(-1) = %f
    ", sqrt(-1));
        show_fe_exceptions();
     
        printf("DBL_MAX*2.0 = %f
    ", DBL_MAX*2.0);
        show_fe_exceptions();
     
        printf("nextafter(DBL_MIN/pow(2.0,52),0.0) = %.1f
    ",
                          nextafter(DBL_MIN/pow(2.0,52),0.0));
        show_fe_exceptions();
    }

    可能的输出:

    MATH_ERREXCEPT is set
    0.0/0.0 = nan
    exceptions raised: FE_INVALID
    1.0/0.0 = inf
    exceptions raised: FE_DIVBYZERO
    1.0/10.0 = 0.100000
    exceptions raised: FE_INEXACT
    sqrt(-1) = -nan
    exceptions raised: FE_INVALID
    DBL_MAX*2.0 = inf
    exceptions raised: FE_INEXACT FE_OVERFLOW
    nextafter(DBL_MIN/pow(2.0,52),0.0) = 0.0
    exceptions raised: FE_INEXACT FE_UNDERFLOW

    参考

    C11标准(ISO / IEC 9899:2011): 7.6 / 6浮点环境<fenv.h>(p:207) C99标准(ISO / IEC 9899:1999): 7.6 / 5浮点环境<fenv.h>(p:188)

    扩展内容

    math_errhandlingMATH_ERRNOMATH_ERREXCEPT(C99)(C99)(C99) 定义了常用数学函数(宏常量)使用的错误处理机制,

    | 用于浮点异常宏的C ++文档 |

  •   C 语言中文开发手册
    ]
    转载请保留页面地址:https://www.breakyizhan.com/c-3/27283.html
  • 相关阅读:
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
  • 原文地址:https://www.cnblogs.com/breakyizhan/p/13286275.html
Copyright © 2011-2022 走看看