Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

rounding

c99

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 ?

like image 965
Martin J. Avatar asked Apr 28 '14 14:04

Martin J.


People also ask

Does round return an int?

Round(decimal) will always return an integral value, these overloads still cannot return an integer value type.

Does Int always round down C++?

The int function returns an integer, so x gets the value 3. Converting to an integer always rounds down, even if the fraction part is 0.99999999. For every type in C++, there is a corresponding function that typecasts its argument to the appropriate type.

Does casting to int round down Python?

It does not round, it just returns the integral part before the decimal point.

What does rounded to the nearest integer mean?

Rounding to the nearest integer is really rounding to the nearest units place. Sometimes, you will be asked to round to the nearest hundreds, or to the nearest hundredths — to some decimal place other than the units place.


1 Answers

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.

like image 57
alk Avatar answered Oct 21 '22 11:10

alk