English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++The llrint() function uses the current rounding mode to round the parameter to an integer value.
The current rounding mode is determined by the fesetround() function. It is similar tolrint()But returns long long int instead of long int.
long long int llrint(double x); long long int llrint(float x); long long int llrint(long double x); long long int llrint(T x); //For integer
The llrint() function takes a single parameter and returns a value of type long long int. This function is in<cmath>Defined in the header file.
The llrint() function truncates a single parameter value.
The llrint() function rounds the parameter x to an integer value using the rounding direction specified by fegetround() and returns the value in the form of long long int.
By default, the rounding direction is set to 'to-nearest'.
You can use the fesetround() function to set the rounding direction to other values.
#include <iostream> #include <cmath> #include <cfenv> using namespace std; int main() { //By default, the rounding direction is the nearest direction, i.e., fesetround(FE_TONEAREST) double x = 11.87; long long int result; result = llrint(x); cout << "Round to the nearest (" << x << ") = " << result << endl; //Midpoint rounded up to a higher integer x = 11.5; result = llrint(x); cout << "Round to the nearest (" << x << ") = " << result << endl; // Set the rounding direction to DOWNWARD fesetround(FE_DOWNWARD); x = 11.87; result = llrint(x); cout << "Round Down (" << x << ") = " << result << endl; // Set the rounding direction to UPWARD fesetround(FE_UPWARD); x = 33.32; result = llrint(x); cout << "Round up (" << x << ") = " << result << endl; return 0; }
When running the program, the output is:
Round to the nearest (11.87) = 12 Round to the nearest (11.5) = 12 Round Down (11.8699) = 11 Round up (33.3201) = 34
#include <iostream> #include <cmath> #include <cfenv> using namespace std; int main() { int x = 15; long long int result; //Set the rounding direction to DOWNWARD fesetround(FE_DOWNWARD); result = llrint(x); cout << "Round Down (" << x << ") = " << result << endl; return 0; }
When running the program, the output is:
Round Down (15) = 15
For integer values, the llrint function will return the same value as the input. Therefore, it is not commonly used to represent integer values in practice.