为什么没有round() - 类型的函数返回一个int?

时间:2014-04-28 14:10:38

标签: c rounding c99

C标准库N1256定义了一堆舍入函数。基本上有两个“完整”的家庭,

  • rint:

    double rint(double x);
    float rintf(float x);
    long double rintl(long double x);
    // The rint functions round their argument to an integer value
    //  in floating-point format, using the current rounding direction
    
    long int lrint(double x);
    long int lrintf(float x);
    long int lrintl(long double x);
    long long int llrint(double x);
    long long int llrintf(float x);
    long long int llrintl(long double x);
    // The lrint and llrint functions round their argument 
    //  to the nearest integer value, rounding according to the current
    //  rounding direction. If the rounded value is outside the range of
    //  the return type, the numeric result is unspecified and a domain
    //  error or range error may occur. *
    
  • 轮:

    double round(double x);
    float roundf(float x);
    long double roundl(long double x);
    // The round functions round their argument to the nearest integer value 
    //  in floating-point format, rounding halfway cases away from zero, 
    //  regardless of the current rounding direction.
    
    long int lround(double x);
    long int lroundf(float x);
    long int lroundl(long double x);
    long long int llround(double x);
    long long int llroundf(float x);
    long long int llroundl(long double x);
    // The lround and llround functions round their argument to the nearest
    //  integer value, rounding halfway cases away from zero, regardless of the
    //  current rounding direction. If the rounded value is outside the range of 
    //  the return type, the numeric result is unspecified and 
    //  a domain error or range error may occur.
    

但是,显然没有任何变体的行为类似于round()rint()并返回int,例如:

int iround(double d);
int iroundf(float f);
int iroundl(long double l);

int irint(double d);
int irintf(float f);
int irintl(long double l);

我理解一个问题可能是int无法表达适当宽范围的值,但long int可以很好地做出相同的论证(最大值为~10 ^ 19 a long int,vs {10} 128 float) 有谁知道为什么标准没有定义这样的舍入函数?

1 个答案:

答案 0 :(得分:7)

  

为什么没有round() - 类型的函数返回一个int?

因为l* - 函数涵盖了i*函数' functionallity。

所以后续问题是:

为什么我们还拥有l*个功能,因为我们还拥有ll*个功能?

答案是C99添加了新的long long数据类型,并附带了相关的新库函数。

对于舍入整数,C90已经提供了

  • lrint*()
  • lround*()

由于兼容的原因需要保持的功能。

使用新的C99数据类型long long新功能

  • llrint*()
  • llround*()

被介绍了。