Domanda

The C standard library, N1256 defines a bunch of rounding functions. There are basically two "complete" families,

  • 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. *
    
  • round:

    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.
    

However, there is apparently no variation that behaves like round() or rint() and returns an int, e.g.:

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);

I understand that one concern may be that int can not express a properly wide range of values, but the same argument could very well be made for long int (maximum value is ~10^19 for a long int, vs ~10^128 for a float).
Does anyone know why the standard does not define such rounding functions ?

È stato utile?

Soluzione

Why isn't there a round()-type function that returns an int?

Because the l*-functions cover the i* functions' functionallity.

So the follow-up question is:

Why do we have l* functions as we also have ll*functions?

The answer is that C99 added the new long long data type, accompanied by the related new library functions.

For rounding integers C90 already provided the

  • lrint*()
  • lround*()

functions which needed to stay around for compatiblitiy reasons.

With the new C99 data type long long the new functions

  • llrint*()
  • llround*()

were introduced.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top