llrint() Function
The llrint()
function rounds a floating-point value to the nearest integer according to the current rounding mode, then casts and returns the result as a long long integer. It performs the rounding operation based on the environment’s rounding direction without explicitly naming the parameter involved.
Syntax of llrint()
long long int llrint(double x);
long long int llrintf(float x);
long long int llrintl(long double x);
Parameters
Parameter | Description |
---|---|
x | Floating-point value to be rounded. |
The function rounds the provided value using the current rounding mode set by the system (which can be modified with functions from fenv.h
). If the resulting value is out of the representable range of a long long integer, the behavior is unspecified; errors may be signaled by setting global error variables or raising floating-point exceptions.
Examples for llrint()
Example 1: Rounding a Positive Floating-Point Number
This example demonstrates how llrint()
rounds a positive floating-point value to the nearest integer.
Program
#include <stdio.h>
#include <math.h>
int main() {
double value = 3.7;
long long int result;
// Rounding the positive floating-point value
result = llrint(value);
printf("Rounded result: %lld\n", result);
return 0;
}
Explanation:
- A floating-point variable is initialized with the value
3.7
. - The
llrint()
function rounds this value to the nearest integer based on the current rounding mode. - The rounded value is cast to a long long integer and stored in
result
. - The resulting integer is printed.
Program Output:
Rounded result: 4
Example 2: Rounding a Negative Floating-Point Number
This example demonstrates how llrint()
handles a negative floating-point value by rounding it to the nearest integer.
Program
#include <stdio.h>
#include <math.h>
int main() {
double value = -2.3;
long long int result;
// Rounding the negative floating-point value
result = llrint(value);
printf("Rounded result: %lld\n", result);
return 0;
}
Explanation:
- A floating-point variable is initialized with the value
-2.3
. - The
llrint()
function rounds this value according to the current rounding mode. - The rounded result is cast to a long long integer and stored in
result
. - The resulting integer is printed.
Program Output:
Rounded result: -2