rint() Function

The rint() function in C rounds a floating-point number to its nearest integral value based on the current rounding mode. It uses the rounding direction set by fegetround() and may raise an FE_INEXACT exception if the rounded value differs from the original.


Syntax of rint()

</>
Copy
double rint(double x);
float rintf(float x);
long double rintl(long double x);

Parameters

ParameterDescription
xThe value to be rounded.

Return Value

The function returns the value of x rounded to a nearby integral value as a floating-point number. If the result differs from x, an FE_INEXACT exception may be raised depending on the implementation.

The function’s behavior depends on the current rounding mode, and it is available via the header <math.h> (or <tgmath.h> for type-generic macros).


Examples for rint()

Example 1: Basic Rounding to Nearest Integral Value

This example demonstrates how rint() rounds a positive floating-point number to its nearest integral value.

Program

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

int main() {
    double num = 3.7;
    double rounded = rint(num);
    printf("Rounded value: %.0f\n", rounded);
    return 0;
}

Explanation:

  1. The program initializes a double variable num with the value 3.7.
  2. rint() rounds the value to the nearest integral value, storing the result in rounded.
  3. The result is printed using printf().

Program Output:

Rounded value: 4

Example 2: Rounding a Negative Floating-Point Number

This example shows how rint() handles the rounding of a negative number according to the current rounding mode.

Program

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

int main() {
    double num = -2.3;
    double rounded = rint(num);
    printf("Rounded value: %.0f\n", rounded);
    return 0;
}

Explanation:

  1. A double variable num is initialized with -2.3.
  2. rint() rounds the value to the nearest integer, and the result is stored in rounded.
  3. The rounded result is printed to the console.

Program Output:

Rounded value: -2

Example 3: Using the Float Variant rintf() for Rounding

This example demonstrates how to use the rintf() function to round a float value to its nearest integral value.

Program

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

int main() {
    float num = 5.5f;
    float rounded = rintf(num);
    printf("Rounded value: %.0f\n", rounded);
    return 0;
}

Explanation:

  1. A float variable num is set to 5.5f.
  2. rintf() rounds the value to the nearest integer, storing the result in rounded.
  3. The output is printed to demonstrate the rounded value.

Program Output:

Rounded value: -2

Example 4: Rounding a Long Double Using rintl()

This example illustrates the use of the rintl() function to round a long double value.

Program

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

int main() {
    long double num = 7.8L;
    long double rounded = rintl(num);
    printf("Rounded value: %.0Lf\n", rounded);
    return 0;
}

Explanation:

  1. A long double variable num is initialized with the value 7.8L.
  2. rintl() rounds the number to its nearest integral value, storing the result in rounded.
  3. The rounded result is printed using printf(), demonstrating the use of the long double variant.

Program Output:

Rounded value: 8