©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
在头文件<fenv.h>中定义 | ||
---|---|---|
int fegetenv(fenv_t * envp); | (1) | (自C99以来) |
int fesetenv(const fenv_t * envp); | (2) | (自C99以来) |
1)试图将浮点环境的状态存储在envp指向的对象中。
2)尝试从envp指向的对象中建立浮点环境。 该对象的值必须事先通过调用feholdexcept或fegetenv获得,或者是浮点宏常量。 如果在envp中设置了任何浮点状态标志,它们将在环境中设置(然后可用fetestexcept进行测试),但不会引发相应的浮点异常(执行继续不间断)。
envp | - | pointer to the object of type fenv_t which holds the status of the floating-point environment |
---|
成功时为0,否则为非零。
#include <stdio.h>#include <math.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){ fenv_t curr_env; int rtn; /* Show default environment. */ show_fe_environment(); printf("\n"); /* Perform some computation under default environment. */ printf("+11.5 -> %+4.1f\n", rint(+11.5)); /* midway between two integers */ printf("+12.5 -> %+4.1f\n", rint(+12.5)); /* midway between two integers */ show_fe_environment(); printf("\n"); /* Save current environment. */ rtn = fegetenv(&curr_env); /* Perform some computation with new rounding method. */ feclearexcept(FE_ALL_EXCEPT); fesetround(FE_DOWNWARD); printf("1.0/0.0 = %f\n", 1.0/0.0); printf("+11.5 -> %+4.1f\n", rint(+11.5)); printf("+12.5 -> %+4.1f\n", rint(+12.5)); show_fe_environment(); printf("\n"); /* Restore previous environment. */ rtn = fesetenv(&curr_env); show_fe_environment(); return 0;}
输出:
current exceptions raised: none current rounding method: FE_TONEAREST +11.5 -> +12.0+12.5 -> +12.0current exceptions raised: FE_INEXACT current rounding method: FE_TONEAREST 1.0/0.0 = inf+11.5 -> +11.0+12.5 -> +12.0current exceptions raised: FE_DIVBYZERO FE_INEXACT current rounding method: FE_DOWNWARD current exceptions raised: FE_INEXACT current rounding method: FE_TONEAREST
C11标准(ISO / IEC 9899:2011):
7.6.4.1 fegetenv函数(p:213)
7.6.4.3 fesetenv函数(p:214)
C99标准(ISO / IEC 9899:1999):
7.6.4.1 fegetenv函数(p:194)
7.6.4.3 fesetenv函数(p:195)
feholdexcept(C99) | 保存环境,清除所有状态标志并忽略所有将来的错误(功能) |
---|---|
feupdateenv(C99) | 恢复浮点环境并引发以前引发的异常(函数) |
FE_DFL_ENV(C99) | 默认浮点环境(宏常量) |
| fegetenv,fesetenv的C ++文档 |