lround() Function

The lround() function rounds a floating-point value to the nearest integer, following the rule of rounding away from zero when the value is exactly halfway between two integers. It is part of the C math library and returns the result as a long integer.


Syntax of lround()

</>
Copy
long int lround(double x);

Parameters

ParameterDescription
xFloating-point value to be rounded to the nearest integer.

Return Value

The function returns the value of x rounded to the nearest integral value as a long int. In cases where x is exactly halfway between two integers, the result is rounded away from zero. Note that if the rounded value exceeds the range of long int, the behavior is unspecified and may trigger error handling based on the implementation.

Additional points to consider include potential domain or overflow errors. If such errors occur and the implementation supports error handling (via errno or floating-point exceptions), appropriate error indicators will be set. The header <tgmath.h> also provides type-generic macro versions of this function.


Examples for lround()

Example 1: Rounding a Positive Floating-Point Value

This example demonstrates rounding a positive floating-point number using lround().

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 3.7;
    long int result = lround(x);
    printf("Rounded result: %ld\n", result);
    return 0;
}

Explanation:

  1. The variable x is initialized with the value 3.7.
  2. The lround() function rounds 3.7 to the nearest integer, resulting in 4.
  3. The rounded result is stored in the variable result and then printed.

Program Output:

Rounded result: 4

Example 2: Rounding a Negative Floating-Point Value

This example shows how lround() handles a negative floating-point number.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = -2.3;
    long int result = lround(x);
    printf("Rounded result: %ld\n", result);
    return 0;
}

Explanation:

  1. The variable x is set to -2.3.
  2. The lround() function rounds -2.3 to the nearest integer, yielding -2.
  3. The result is stored and printed, demonstrating rounding of negative values.

Program Output:

Rounded result: -2

Example 3: Rounding Halfway Cases Away from Zero

This example demonstrates the behavior of lround() when the value is exactly halfway between two integers.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main(void) {
    double x1 = 2.5;
    double x2 = -2.5;
    long int result1 = lround(x1);
    long int result2 = lround(x2);
    printf("Rounded 2.5: %ld\n", result1);
    printf("Rounded -2.5: %ld\n", result2);
    return 0;
}

Explanation:

  1. The variables x1 and x2 are set to 2.5 and -2.5, respectively.
  2. The function rounds 2.5 away from zero to 3, and -2.5 away from zero to -3.
  3. The rounded results are then printed.

Program Output:

Rounded 2.5: 3
Rounded -2.5: -3

Example 4: Using lroundf() and lroundl() Variants

This example illustrates the use of the variants lroundf() for float values and lroundl() for long double values.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main(void) {
    float xf = 5.8f;
    long double xl = 7.2L;
    long int resf = lroundf(xf);
    long int resl = lroundl(xl);
    printf("Rounded float 5.8: %ld\n", resf);
    printf("Rounded long double 7.2: %ld\n", resl);
    return 0;
}

Explanation:

  1. A float value xf and a long double value xl are initialized.
  2. lroundf() is used to round the float, while lroundl() rounds the long double.
  3. The results are stored in separate variables and then printed.

Program Output:

Rounded float 5.8: 6
Rounded long double 7.2: 7