FE_DFL_ENV

来自cppreference.com
< c‎ | numeric‎ | fenv
定义于头文件 <fenv.h>
#define FE_DFL_ENV  /*implementation defined*/
(C99起)

宏常量FE_DFL_ENV展开成const fenv_t*类型的表达式,它指向默认浮点环境的完整复制,即该环境是在程序启动时加载的。

FE_跟随大写字母为首,并拥有const fenv_t*类型的附加性宏,可能会为实现所支持。

[编辑] 示例

#include <stdio.h>
#include <fenv.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current 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");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
void show_fe_rounding_method(void)
{
    printf("current rounding method:    ");
    switch (fegetround()) {
           case FE_TONEAREST:  printf ("FE_TONEAREST");  break;
           case FE_DOWNWARD:   printf ("FE_DOWNWARD");   break;
           case FE_UPWARD:     printf ("FE_UPWARD");     break;
           case FE_TOWARDZERO: printf ("FE_TOWARDZERO"); break;
           default:            printf ("unknown");
    };
    printf("\n");
}
 
void show_fe_environment(void)
{
    show_fe_exceptions();
    show_fe_rounding_method();
} 
 
int main(void)
{
    printf("On startup:\n");
    show_fe_environment();
 
    // 更改环境
    fesetround(FE_DOWNWARD);     // 更改舍入模式
    feraiseexcept(FE_INVALID);   // 引发异常
    printf("\nBefore restoration:\n");
    show_fe_environment();
 
    fesetenv(FE_DFL_ENV);    // 恢复
    printf("\nAfter restoring default environment:\n");
    show_fe_environment();
}

输出:

On startup:
current exceptions raised:  none
current rounding method:    FE_TONEAREST
 
Before restoration:
current exceptions raised:  FE_INVALID
current rounding method:    FE_DOWNWARD
 
After restoring default environment:
current exceptions raised:  none
current rounding method:    FE_TONEAREST

[编辑] 参考

  • C11 standard (ISO/IEC 9899:2011):
  • 7.6/9 Floating-point environment <fenv.h> (p: 208)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.6/8 Floating-point environment <fenv.h> (p: 188-189)

[编辑] 参阅

保存或恢复当前浮点环境
(函数) [edit]
恢复浮点环境并引发之前引发过的异常
(函数) [edit]
FE_DFL_ENVC++文档