C++ std::lrint() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::lrint(x) rounds a decimal and gives you a whole number type, a long. It uses the current rounding mode, which is normally "to the nearest, with halves going to the even number". So lrint(2.5) is 2 and lrint(3.5) is 4.

Compared with rint(), there is no need for a cast. The answer is ready to use as a count or an index. It is also better than (long)x, which cuts the decimals and does not round. If the number is too big for a long, the result is not defined, so check the range first.

std::lrint() is declared in <cmath> and is part of the group Rounding. Rounding functions turn a fractional number into a whole number. They differ in which way they go for negative numbers and for ties such as 2.5.

Syntax

Include <cmath> and call std::lrint. Start with these lines.

C++
long a = std::lrint(2.5);    // 2  (even)
long b = std::lrint(3.5);    // 4  (even)
long c = std::lrint(-2.7);   // -3
CallWhat it does
std::lrint(2.5)Exactly halfway. The even neighbour 2 is chosen.
std::lrint(2.7)Closer to 3, so the result is 3.
std::lrint(2.5f)The float overload also returns a long.

The return type is always long, whatever the input type. Use llrint() for long long.

Library declarations
C++
long lrint(float x);
long lrint(double x);
long lrint(long double x);
long lrint(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. The number to round. It can be float, double, long double, or an integer type (converted to double).

Return Value

Return ValueDescription
longThe nearest whole number according to the current rounding mode.
UnspecifiedIf the result is out of range for a long, or x is NaN or infinity.

C++ Examples

Example 1 — Round Several Numbers to a long

The results are whole numbers of type long, so they print without a decimal point.

C++
#include <cmath>
#include <iostream>

int main() {
    double values[] = {2.5, 3.5, -2.5, 2.7, -0.2};

    for (double v : values) {
        long r = std::lrint(v);
        std::cout << "lrint(" << v << ") = " << r << '\n';
    }
    return 0;
}

How It Works

  1. Each value is rounded to the nearest long. The type of r is long, so no cast is needed.
  2. 2.5 goes to the even 2, and 3.5 goes to the even 4. -2.5 also goes to the even -2.
  3. 2.7 rounds to 3. -0.2 rounds to 0, and as a long there is no such thing as negative zero.

Example 2 — Round a Position to a Pixel

Screen pixels are whole numbers. Round each decimal position to the nearest pixel.

C++
#include <cmath>
#include <iostream>

int main() {
    double positions[] = {12.49, 12.5, 12.51};

    for (double x : positions) {
        long pixel = std::lrint(x);
        std::cout << "x = " << x << " -> pixel " << pixel << '\n';
    }
    return 0;
}

How It Works

  1. The three positions are very close, but the pixel is the nearest whole number to each one.
  2. 12.49 is below the half, so the pixel is 12. 12.51 is above it, so the pixel is 13.
  3. 12.5 is exactly halfway, and 12 is the even neighbour, so it becomes 12 too. With lround() it would be 13.

Example 3 — Convert Only When the Value Fits

If the value is too big for a long, the result is undefined. Check the number first, with a small helper function.

C++
#include <cmath>
#include <iostream>
#include <limits>

bool safeRound(double x, long& out) {
    if (!std::isfinite(x)) return false;
    double limit = static_cast<double>(std::numeric_limits<long>::max());
    if (std::abs(x) >= limit) return false;
    out = std::lrint(x);
    return true;
}

int main() {
    double inputs[] = {1234.5, 1e20, std::nan("")};

    for (double x : inputs) {
        long result = 0;
        if (safeRound(x, result)) {
            std::cout << "ok: " << result << '\n';
        } else {
            std::cout << "cannot convert\n";
        }
    }
    return 0;
}

How It Works

  1. safeRound() first rejects infinity and NaN with std::isfinite(). Then it checks that |x| is below the biggest long.
  2. 1234.5 passes both tests. It is exactly halfway, so lrint() picks the even neighbour 1234.
  3. 1e20 is far too big for a long, and NaN is not a number. Both fail the check, so the program prints cannot convert and never calls lrint().

Frequently Asked Questions

std::lrint(x) rounds x to a whole number using the current rounding mode and returns it as a long. By default halves go to the even number, so lrint(2.5) is 2.
The rounded value as a long. If the result does not fit in a long, or x is NaN or infinity, the value is unspecified and an invalid exception is raised.
Both return a long. lrint() follows the current rounding mode, and halves go to the even number by default. lround() always sends halves away from zero.
The result is unspecified and a floating-point invalid exception is raised. No error is thrown. Check the range yourself, or use llrint() for a bigger type.
Include <cmath> and write std::lrint(). That always works and picks the C++ overloads. The global name ::lrint often works too, but the standard does not promise it, and it may only take a double.

Next: lround()

Keep going through the <cmath> functions. Round to the nearest long, halves away from zero.

lround() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful