C Reference ยท ctype.h, math.h and others
C lround() and nearbyint() functions
Reference for C lround(), llround(), nearbyint() and rint() from math.h: round a double straight to an integer type, ties-to-even and the rounding mode.
lround() rounds a double to the nearest integer and returns it as a long; llround() returns a long long. Halfway cases go away from zero, exactly like round. They save you the cast that (long)round(x) would need and make the intent clear.
nearbyint() and rint() round to a whole number but return a double. They obey the current floating-point rounding mode, which by default rounds halfway cases to the nearest even number ("banker's rounding"). lrint() and llrint() are their integer-returning forms.
#include <math.h> long lround(double x); long long llround(double x); double nearbyint(double x); long lrint(double x);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
long lround(double x); long long llround(double x); double nearbyint(double x); double rint(double x); long lrint(double x); long long llrint(double x);
f and l suffixes exist too (lroundf, nearbyintl, ...).
| Name | Description |
|---|---|
x |
The value to round. |
Returns: lround and llround return the nearest integer, with halves rounded away from zero, whatever the current rounding mode. nearbyint returns the nearest whole number as a double according to the current rounding mode. lrint and llrint do the same but return integers. rint behaves like nearbyint, except that it may raise the "inexact" floating-point exception.
Errors / undefined behavior: if x is NaN, infinite or too large for the return type, the result is unspecified and a domain or range error may be reported (FE_INVALID, errno set to EDOM or ERANGE, depending on math_errhandling). Check the range first when the input is not trusted.
Usage notes
- Which one? For "round to the nearest integer as people expect" use
lround. For numerical code that should not drift (summing many values), the unbiased ties-to-even behavior oflrint/nearbyintis better. - Halfway cases. 0.5, 1.5, 2.5, 3.5 become 1, 2, 3, 4 with
lround, but 0, 2, 2, 4 withnearbyintin the default mode. - Rounding mode.
fesetround()from<fenv.h>changes it (FE_TONEAREST,FE_UPWARD,FE_DOWNWARD,FE_TOWARDZERO).nearbyint,rintandlrintfollow it;roundandlrounddo not. Changing the mode is global to the thread, so restore it when done. Seefenv.h. rintversusnearbyint: the only difference is the "inexact" flag, whichnearbyintnever raises.- Choosing the return type.
longis 64 bits on 64-bit Linux and macOS, but 32 bits on 64-bit Windows. Usellroundif you need 64 bits everywhere, and<stdint.h>types for the final storage. - Conversions are cheaper. On common hardware
lrintcompiles to a single instruction, which makes it the fastest way to convert a double to the nearest integer (when-fno-math-errnoor similar flags allow it). - Casting is not rounding.
(long)xtruncates toward zero. - Standard version. All of these arrived in C99.