round函数c语言,fegetround

在头文件中定义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 #include #include 

#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 ++文档 |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值