C
数值 | Numerics

fegetround

fegetround, fesetround

在头文件中定义
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 ON void 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.0 current rounding method: FE_DOWNWARD +11.5 -> +11.0 +12.5 -> +12.0 current rounding method: FE_TONEAREST

参考

  • C11标准(ISO / IEC 9899:2011):

扩展内容

nearbyintnearbyintfnearbyintl (C99)(C99)(C99) 使用当前舍入模式(函数)舍入为整数
rintrintfrintllrintlrintflrintlllrintllrintfllrintl (C99)(C99)(C99)(C99)(C99)(C99)(C99)(C99)(C99) 使用当前舍入模式舍入为整数,除非结果不同(函数)

| 用于fegetround,fesetround 的C ++文档 |