c语言round的用法,roundl - [ C语言中文开发手册 ] - 在线原生手册 - php中文网

在头文件中定义float       roundf( float arg );(1)(since C99)

double      round( double arg );(2)(since C99)

long double roundl( long double arg );(3)(since C99)

Defined in header

#define round( arg )(4)(since C99)

Defined in header

long      lroundf( float arg );(5)(since C99)

long      lround( double arg );(6)(since C99)

long      lroundl( long double arg );(7)(since C99)

Defined in header

#define lround( arg )(8)(since C99)

Defined in header

long long llroundf( float arg );(9)(since C99)

long long llround( double arg );(10)(since C99)

long long llroundl( long double arg );(11)(since C99)

Defined in header

#define llround( arg )(12)(since C99)

1-3)计算最接近的整数值arg(以浮点格式),无论当前的舍入模式如何,将距离零的中途情况四舍五入。

5-7,9-11)arg无论当前的舍入模式如何,计算最接近的整数值(以整数格式),从零中途舍入一半。

4,8,12)类型泛型宏:如果arg有一个类型long double,roundl,lroundl,llroundl被调用。否则,如果arg有整数类型或类型double,round,lround,llround被调用。否则roundf,lroundf,llroundf叫,分别。

参数

arg-浮点值

返回值

如果没有发生错误arg,则返回距离零中途的四舍五入的最近整数值。

返回值

论据

如果发生域错误,则返回实现定义的值。

错误处理

按照math_errhandling中的指定报告错误。

如果返回类型表示的范围lround或结果llround超出范围,则可能会出现域错误或范围错误。

如果实现支持IEEE浮点运算(IEC 60559),对于round,roundf和roundl功能:

当前的舍入模式不起作用。

如果arg是±∞,则返回,未修改

如果arg为±0,则返回,未修改

如果arg是NaN,则返回NaN

对于lround和llround功能家庭:

FE_INEXACT 从未被提出

当前的舍入模式不起作用。

如果arg是±∞,FE_INVALID则引发并返回实现定义的值

如果舍入的结果超出返回类型的范围,FE_INVALID则会引发并返回实现定义的值

如果arg是NaN,FE_INVALID则引发并返回实现定义的值

笔记

FE_INEXACT可能是(但不要求)round在舍入非整数有限值时引发。

最大的可表示浮点值是所有标准浮点格式中的精确整数,因此round不会自行溢出; 但是intmax_t,当存储在整数变量中时,结果可能会溢出任何整数类型(包括)。

POSIX规定是,所有病例lround或llround提高FE_INEXACT是域错误。

该行为的double版本round如同执行如下:

#include #include #pragma STDC FENV_ACCESS ON

double round(double x){

fenv_t save_env;    feholdexcept(&save_env);

double result = rint(x);    if (fetestexcept(FE_INEXACT)) {        fesetround(FE_TOWARDZERO);

result = rint(copysign(0.5 + fabs(x), x));    }    feupdateenv(&save_env);    return result;}

#include #include #include #include 

#pragma STDC FENV_ACCESS ON

int main(void){    // round    printf("round(+2.3) = %+.1f  ", round(2.3));    printf("round(+2.5) = %+.1f  ", round(2.5));    printf("round(+2.7) = %+.1f\n", round(2.7));    printf("round(-2.3) = %+.1f  ", round(-2.3));    printf("round(-2.5) = %+.1f  ", round(-2.5));    printf("round(-2.7) = %+.1f\n", round(-2.7));

printf("round(-0.0) = %+.1f\n", round(-0.0));    printf("round(-Inf) = %+f\n",   round(-INFINITY));

// lround    printf("lround(+2.3) = %ld  ", lround(2.3));    printf("lround(+2.5) = %ld  ", lround(2.5));    printf("lround(+2.7) = %ld\n", lround(2.7));    printf("lround(-2.3) = %ld  ", lround(-2.3));    printf("lround(-2.5) = %ld  ", lround(-2.5));    printf("lround(-2.7) = %ld\n", lround(-2.7));

printf("lround(-0.0) = %ld\n", lround(-0.0));    printf("lround(-Inf) = %ld\n", lround(-INFINITY)); // FE_INVALID raised

// error handling    feclearexcept(FE_ALL_EXCEPT);    printf("lround(LONG_MAX+1.5) = %ld\n", lround(LONG_MAX+1.5));    if(fetestexcept(FE_INVALID)) puts("    FE_INVALID was raised");}

可能的输出:

round(+2.3) = +2.0  round(+2.5) = +3.0  round(+2.7) = +3.0round(-2.3) = -2.0  round(-2.5) = -3.0  round(-2.7) = -3.0round(-0.0) = -0.0round(-Inf) = -inflround(+2.3) = 2  lround(+2.5) = 3  lround(+2.7) = 3lround(-2.3) = -2  lround(-2.5) = -3  lround(-2.7) = -3lround(-0.0) = 0lround(-Inf) = -9223372036854775808lround(LONG_MAX+1.5) = -9223372036854775808

FE_INVALID was raised

参考

C11标准(ISO / IEC 9899:2011):7.12.9.6轮次函数(p:253)

7.12.9.7地下和地下功能(p:253)

7.25类型通用数学(p:373-375)

F.10.6.6循环函数(p:527)

F.10.6.7基础和基本功能(p:528)

C99标准(ISO / IEC 9899:1999):7.12.9.6轮次函数(p:233)

7.12.9.7理论和实践(p:234)

7.22类型通用数学(p:335-337)

F.9.6.6循环函数(p:464)

F.9.6.7地下和地下功能(p:464)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值