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()
double rint(double x);
float rintf(float x);
long double rintl(long double x);
Parameters
Parameter | Description |
---|---|
x | The 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
#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:
- The program initializes a double variable
num
with the value3.7
. rint()
rounds the value to the nearest integral value, storing the result inrounded
.- 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
#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:
- A double variable
num
is initialized with-2.3
. rint()
rounds the value to the nearest integer, and the result is stored inrounded
.- 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
#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:
- A float variable
num
is set to5.5f
. rintf()
rounds the value to the nearest integer, storing the result inrounded
.- 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
#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:
- A long double variable
num
is initialized with the value7.8L
. rintl()
rounds the number to its nearest integral value, storing the result inrounded
.- The rounded result is printed using
printf()
, demonstrating the use of the long double variant.
Program Output:
Rounded value: 8