©
This document uses PHP Chinese website manual Release
在头文件<fenv.h>中定义 | ||
---|---|---|
int fesetround(int round); | (1) | (自C99以来) |
int fegetround(); | (2) | (自C99以来) |
1)尝试建立等于参数参数轮的浮点舍入方向,该循环方向预计是浮点舍入宏之一。
2)返回对应于当前舍入方向的浮点舍入宏的值。
round | - | rounding direction, one of floating point rounding macros |
---|
1)成功时为0,否则为非零。
2)描述当前舍入方向的浮点舍入宏,或者如果方向不能确定,则为负值。
当前的舍入模式反映了最近的情况,也可以用FLT_ROUNDS查询。
#include <stdio.h>#include <math.h>#include <fenv.h> #pragma STDC FENV_ACCESS ONvoid show_fe_current_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");} int main(void){ /* Default rounding method */ show_fe_current_rounding_method(); 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 */ /* Save current rounding method. */ int curr_method = fegetround(); /* Temporarily change current rounding method. */ fesetround(FE_DOWNWARD); show_fe_current_rounding_method(); printf("+11.5 -> %+4.1f\n", rint(+11.5)); printf("+12.5 -> %+4.1f\n", rint(+12.5)); /* Restore default rounding method. */ fesetround(curr_method); show_fe_current_rounding_method(); return 0;}
可能的输出:
current rounding method: FE_TONEAREST+11.5 -> +12.0+12.5 -> +12.0current rounding method: FE_DOWNWARD+11.5 -> +11.0+12.5 -> +12.0current rounding method: FE_TONEAREST
C11标准(ISO / IEC 9899:2011):
7.6.3.1 fegetround函数(p:212)
7.6.3.2 fesetround函数(p:212-213)
C99标准(ISO / IEC 9899:1999):
7.6.3.1 fegetround函数(p:193)
7.6.3.2 fesetround函数(p:193-194)
nearbyintnearbyintfnearbyintl (C99)(C99)(C99) | 使用当前舍入模式(函数)舍入为整数 |
---|---|
rintrintfrintllrintlrintflrintlllrintllrintfllrintl (C99)(C99)(C99)(C99)(C99)(C99)(C99)(C99)(C99) | 使用当前舍入模式舍入为整数,除非结果不同(函数) |
| 用于fegetround,fesetround 的C ++文档 |