📜  C++ lround()(1)

📅  最后修改于: 2023-12-03 14:39:50.039000             🧑  作者: Mango

C++ lround()

The lround() function in C++ is used to round off a given floating-point value to the nearest long integer value. It is a part of the cmath header file and returns a long integer that is closest to the given argument.

Syntax

The syntax for the lround() function is:

long lround(double x);

where x is the floating-point value to be rounded off.

Return Value

The lround() function returns the rounded off value of x which is a long integer.

Example
#include <iostream>
#include <cmath>
using namespace std;

int main() {
   double num = 3.6;
   long rounded_num = lround(num);

   cout << "The rounded off value of " << num << " is " << rounded_num << endl;

   return 0;
}

Output:

The rounded off value of 3.6 is 4
Some Important Points
  • The lround() function rounds off the value of x to the nearest integer using the "round to even" rule.
  • The range of values that can be rounded off using lround() function is [-2^31, 2^31 - 1] on a 32-bit system and [-2^63, 2^63 - 1] on a 64-bit system.
  • If the value of x is out of range, the behavior of the lround() function is undefined.
  • The lround() function works with the double datatype only.
Conclusion

The lround() function is a useful function provided by C++ to round off a given floating-point value to the nearest long integer value. It is used in scientific and mathematical calculations. It is essential to keep in mind the range of values that can be rounded off using this function and the datatype that it works with.