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

Mar*_* J. 5 c c99 rounding

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. *
    
    Run Code Online (Sandbox Code Playgroud)
  • 回合:

    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.
    
    Run Code Online (Sandbox Code Playgroud)

但是,显然没有任何行为类似于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);
Run Code Online (Sandbox Code Playgroud)

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

alk*_*alk 7

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

因为l*-functions涵盖了i*函数的功能.

所以后续问题是:

为什么我们有l*功能,因为我们也有ll*功能?

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

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

  • lrint*()
  • lround*()

由于兼容的原因需要保持不变的功能.

随着新的C99数据类型long long的新功能

  • llrint*()
  • llround*()

介绍了.