C++ round( ), lround( ) and llround( ) functions














































C++ round( ), lround( ) and llround( ) functions



 C++ round( )
 The round() function in C++ returns the integral value that is nearest to the
argument, with halfway cases rounded away from zero.
       syntax :- round(float x);
                      round(double x);
                      round(long double x);

       parameter :- The round() function takes a floating-point number which
is to be rounded off. It can be of the following types double,
float, long double.

       return value :- The round() function returns the integral value that is
nearest to num, with halfway cases rounded away from
zero.

 C++ lround( )
 The lround() function in C++ rounds the integer value that is nearest to the
argument, with halfway cases rounded away from zero. The value returned is
of type long int. It is similar to the round() function, but returns a long int
whereas round returns the same data type as the input.
       syntax :- lround(double x);
                     lround(float x);
                     lround(long double x);

       parameter :- The lround() function takes a single argument value to round.

       return value :- The lround() function returns the integral value that is
nearest to x, with halfway cases rounded away from zero.
The value returned is of type long int.

 C++ llround( )
 The llround() function in C++ rounds the integer value that is nearest to the
argument, with halfway cases rounded away from zero. The value returned is
of type long long int. It is similar to the lround() function, but returns a long
long int whereas lround returns long int.
       syntax :- llround(double x);
                      llround(float x);
                      llround(long double x);

       parameter :- The llround() function takes a single argument value to
round.

       return value :- The llround() function returns the integral value that is
nearest to x, with halfway cases rounded away from zero.
The value returned is of type long long int.

 Code :-
#include <iostream>
#include <cmath>

using namespace std;

int main() {
double x = 21.18, result1;
long int result2;
long long int result3;

result1 = round(x);
result2 = lround(x);
result3 = llround(x);
cout << "round(" << x << ") = " << result1 << endl;
cout << "lround(" << x << ") = " << result2 << endl;
cout << "llround(" << x << ") = " << result3 << endl;

x =
38.74;
result1 = round(x);
result2 = lround(x);
result3 = llround(x);
cout << "\nround(" << x << ") = " << result1 << endl;
cout << "lround(" << x << ") = " << result2 << endl;
cout << "llround(" << x << ") = " << result3 << endl;

x =
75.5;
result1 = round(x);
result2 = lround(x);
result3 = llround(x);
cout << "\nround(" << x << ") = " << result1 << endl;
cout << "lround(" << x << ") = " << result2 << endl;
cout << "llround(" << x << ") = " << result3 << endl;

x =
-21.18;
result1 = round(x);
result2 = lround(x);
result3 = llround(x);
cout << "\nround(" << x << ") = " << result1 << endl;
cout << "lround(" << x << ") = " << result2 << endl;
cout << "llround(" << x << ") = " << result3 << endl;

x =
-38.74;
result1 = round(x);
result2 = lround(x);
result3 = llround(x);
cout << "\nround(" << x << ") = " << result1 << endl;
cout << "lround(" << x << ") = " << result2 << endl;
cout << "llround(" << x << ") = " << result3 << endl;

x =
-75.5;
result1 = round(x);
result2 = lround(x);
result3 = llround(x);
cout << "\nround(" << x << ") = " << result1 << endl;
cout << "lround(" << x << ") = " << result2 << endl;
cout << "llround(" << x << ") = " << result3 << endl;

return 0;
}


Output :-

round(21.18) = 21
lround(21.18) = 21
llround(21.18) = 21

round(38.74) = 39
lround(38.74) = 39
llround(38.74) = 39

round(75.5) = 76
lround(75.5) = 76
llround(75.5) = 76

round(-21.18) = -21
lround(-21.18) = -21
llround(-21.18) = -21

round(-38.74) = -39
lround(-38.74) = -39
llround(-38.74) = -39

round(-75.5) = -76
lround(-75.5) = -76
llround(-75.5) = -76



Comments