Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <math.h>

long      lround(double x);
long long llround(double x);
double    nearbyint(double x);
long      lrint(double x);

Try it

C Ties away from zero versus ties to even

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.

C The rounding mode and out-of-range values

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

C
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 of lrint/nearbyint is better.
  • Halfway cases. 0.5, 1.5, 2.5, 3.5 become 1, 2, 3, 4 with lround, but 0, 2, 2, 4 with nearbyint in the default mode.
  • Rounding mode. fesetround() from <fenv.h> changes it (FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO). nearbyint, rint and lrint follow it; round and lround do not. Changing the mode is global to the thread, so restore it when done. See fenv.h.
  • rint versus nearbyint: the only difference is the "inexact" flag, which nearbyint never raises.
  • Choosing the return type. long is 64 bits on 64-bit Linux and macOS, but 32 bits on 64-bit Windows. Use llround if you need 64 bits everywhere, and <stdint.h> types for the final storage.
  • Conversions are cheaper. On common hardware lrint compiles to a single instruction, which makes it the fastest way to convert a double to the nearest integer (when -fno-math-errno or similar flags allow it).
  • Casting is not rounding. (long)x truncates toward zero.
  • Standard version. All of these arrived in C99.