round() in C++
Last Updated :
31 Mar, 2023
round is used to round off the given digit which can be in float or double. It returns the nearest integral value to provided parameter in round function, with halfway cases rounded away from zero. Instead of round(), std::round() can also be used . Header files used -> cmath, ctgmath Syntax :
Parameters: x, value to be rounded
double round (double x);
float round (float x);
long double round (long double x);
double round (T x);
// additional overloads for integral types
Returns: The value of x rounded to the nearest
integral (as a floating-point value).
Time Complexity: O(1)
Space Complexity: O(1)
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. It takes the same parameters as that of round( ) function.
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. It also takes the same parameters as that of round( ) function.
C++
< div id= "highlighter_673382" class = "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class = "code" >< div class = "container" >< div class = "line number1 index0 alt2" ><code class = "comments" >
|
Output
...d(0.075) = 0
lround(0.14) = 0 lround(21.48) = 21 lround(0.075) = 0
llround(0.14) = 0 llround(21.48) = 21 llround(0.075) = 0
round(0.85) = 1 round(25.73) = 26 round(91.553) = 92
lround(0.85) = 1 lround(25.73) = 26 lround(91.553) = 92
llround(0.85) = 1 llround(25.73) = 26 llround(91.553) = 92
round(0.5) = 1 round(25.5) = 26 round(91.5) = 92
lround(0.5) = 1 lround(25.5) = 26 lround(91.5) = 92
llround(0.5) = 1 llround(25.5) = 26 llround(91.5) = 92
round(-0.14) = -0 round(-0.002) = -0 round(-23.39) = -23
lround(-0.14) = 0 lround(-0.002) = 0 lround(-23.39) = -23
llround(-0.14) = 0 llround(-0.002) = 0 llround(-23.39) = -23
round(-0.72) = -1 round(-21.864) = -22 round(-57.62) = -58
lround(-0.72) = -1 lround(-21.864) = -22 lround(-57.62) = -58
llround(-0.72) = -1 llround(-21.864) = -22 llround(-57.62) = -58
round(-0.5) = -1 round(-6.5) = -7 round(-56.5) = -57
lround(-0.5) = -1 lround(-6.5) = -7 lround(-56.5) = -57
llround(-0.5) = -1 llround(-6.5) = -7 llround(-56.5) = -57
Here, in the above program we have just calculated the nearest integral value of given float or double value. which has been calculated accurately.
Possible Applications
- Handling the mismatch between fractions and decimal : One use of rounding numbers is shorten all the three’s to the right of the decimal point in converting 1/3 to decimal. Most of the time, we will use the rounded numbers 0.33 or 0.333 when we need to work with 1/3 in decimal. We usually work with just two or three digits to the right of the decimal point when there is no exact equivalent to the fraction in decimal.
- Changing multiplied result : There will be difference between multiplication of 25, 75 and 0.25, 0.75 we get 0.875 .We started with 2 digits to the right of the decimal point and ended up with 4. Many times we will just round up the result to 0.19 .
CPP
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
long int a1 = 25, b1 = 30;
double a2 = .25, b2 = .30;
long int ans_1 = (a1 * b1);
double ans_2 = (a2 * b2);
cout << "From first multiplication :" << round(ans_1) << "\n" ;
cout << "From second multiplication :" << round(ans_2) << "\n" ;
return 0;
}
|
Output
From first multiplication :750
From second multiplication :0
- Fast calculation : Suppose in need of fast calculation we take approx value and then calculate nearest answer. For example, we get an answer 298.78 after any calculation and by rounding off we get an absolute answer of 300.
- Getting estimate : Sometimes you want to round integers instead of decimal numbers. Usually you are interested in rounding to the nearest multiple of 10, 100, 1, 000 or million. For example, in 2006 the census department determined that the population of New York City was 8, 214, 426. That number is hard to remember and if we say the population of New York City is 8 million it is a good estimate because it doesn’t make any real difference what the exact number is.
Official references –
Reference : www.mathworksheetcenter.com, www.cplusplus.com
Share your thoughts in the comments
Please Login to comment...