什么是rounding mode
c语言中rounding mode(rounding direction)
的总结(基于C99标准)。所谓rounding mode就是常说的舍入模式
,例如,四舍五入、向上取整、向下取整。舍入模式
决定了当浮点运算的结果不能在有效值中精确表示时应该如何处理。
rounding mode在C99中的定义
FLT_ROUNDS
表示浮点运算的舍入模式。FLT_ROUNDS
的值如下图(from C99-N1256)所示:
toward zero
表示运算后的结果向0值方向靠近;
to nearest
表示运算后的结果向最近的整数方向靠近;
toward positive infinity
表示正无穷方向;
toward negative infinity
表示负无穷方向;
标准中规定,FLT_ROUNDS
的值要正确的反应当前的rounding mode
,但是在glibc 2.1版本中,fesetround()
并不能正确地改变FLT_ROUNDS
的值,FLT_ROUNDS
的值始终为1。FLT_ROUNDS
在<float.h>中定义。
标准中规定,只有fegetround()
和fesetround()
实现后,FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO表示4种rounding mode
。FE_*宏在<fenv.h>中定义。
FE_TOWARDZERO
等价于FLT_ROUNDS
的toward zero
;
FE_TONEAREST
等价于FLT_ROUNDS
的to nearest
;
FE_UPWARD
等价于FLT_ROUNDS
的toward positive infinity
;
FE_DOWNWARD
等价于FLT_ROUNDS
的toward negative infinity
;
示例
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <fenv.h>
int main(int argc, char** argv)
{
double a = 1.2;
double b = -1.2;
double c = -1.5;
double d = -1.51;
double e = 1.5;
double f = 1.51;
printf("FE_TONEAREST=%d\n", FE_TONEAREST);
printf("FE_UPWARD=%d\n", FE_UPWARD);
printf("FE_DOWNWARD=%d\n", FE_DOWNWARD);
printf("FE_TOWARDZERO=%d\n", FE_TOWARDZERO);
printf("\n");
printf("current rouding mode:%d\n",fegetround());
printf("FLT_ROUNDS=%d\n", FLT_ROUNDS);
printf("rint(%f)=%f\n", a, rint(a));
printf("rint(%f)=%f\n", b, rint(b));
printf("rint(%f)=%f\n", c, rint(c));
printf("rint(%f)=%f\n", e, rint(e));
printf("rint(%f)=%f\n", f, rint(f));
printf("rint(%f)=%f\n", f, rint(f));
printf("1-after set rounding mode\n");
fesetround(FE_TOWARDZERO);
printf("current rouding mode:%d\n",fegetround());
printf("FLT_ROUNDS=%d\n", FLT_ROUNDS);
printf("rint(%f)=%f\n", a, rint(a));
printf("rint(%f)=%f\n", b, rint(b));
printf("rint(%f)=%f\n", c, rint(c));
printf("rint(%f)=%f\n", e, rint(e));
printf("rint(%f)=%f\n", f, rint(f));
printf("rint(%f)=%f\n", f, rint(f));
printf("2-after set rounding mode\n");
fesetround(FE_UPWARD);
printf("current rouding mode:%d\n",fegetround());
printf("FLT_ROUNDS=%d\n", FLT_ROUNDS);
printf("rint(%f)=%f\n", a, rint(a));
printf("rint(%f)=%f\n", b, rint(b));
printf("rint(%f)=%f\n", c, rint(c));
printf("rint(%f)=%f\n", e, rint(e));
printf("rint(%f)=%f\n", f, rint(f));
printf("rint(%f)=%f\n", f, rint(f));
printf("3-after set rounding mode\n");
fesetround(FE_DOWNWARD);
printf("current rouding mode:%d\n",fegetround());
printf("FLT_ROUNDS=%d\n", FLT_ROUNDS);
printf("rint(%f)=%f\n", a, rint(a));
printf("rint(%f)=%f\n", b, rint(b));
printf("rint(%f)=%f\n", c, rint(c));
printf("rint(%f)=%f\n", e, rint(e));
printf("rint(%f)=%f\n", f, rint(f));
printf("rint(%f)=%f\n", f, rint(f));
return 0;
}
rint()函数的作用是 按rounding direction
取整。
运行结果
$ ./a.out
FE_TONEAREST=0
FE_UPWARD=2048
FE_DOWNWARD=1024
FE_TOWARDZERO=3072
current rouding mode:0 # 说明,默认的rounding mode是to nearest
FLT_ROUNDS=1
rint(1.200000)=1.000000
rint(-1.200000)=-1.000000
rint(-1.500000)=-2.000000
rint(1.500000)=2.000000
rint(1.510000)=2.000000
rint(1.510000)=2.000000
1-after set rounding mode
current rouding mode:3072 # 设置为 toword zero
FLT_ROUNDS=1 # FLT_ROUNDS的值没有变化
rint(1.199999)=1.000000
rint(-1.199999)=-1.000000
rint(-1.500000)=-1.000000
rint(1.500000)=1.000000
rint(1.510000)=1.000000
rint(1.510000)=1.000000
2-after set rounding mode
current rouding mode:2048 # 设置为 upward
FLT_ROUNDS=1 # FLT_ROUNDS的值没有变化
rint(1.200000)=2.000000
rint(-1.199999)=-1.000000
rint(-1.500000)=-1.000000
rint(1.500000)=2.000000
rint(1.510001)=2.000000
rint(1.510001)=2.000000
3-after set rounding mode
current rouding mode:1024 # 设置为 downward
FLT_ROUNDS=1 # FLT_ROUNDS的值没有变化
rint(1.199999)=1.000000
rint(-1.200000)=-2.000000
rint(-1.500000)=-2.000000
rint(1.500000)=1.000000
rint(1.510000)=1.000000
rint(1.510000)=1.000000