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
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).
Nearest value of x :13 Nearest value of y :13 Nearest value of z :15 lround(-0.0) = 0 lround(2.3) = 2 lround(2.5) = 3 lround(2.7) = 3 lround(-2.3) = -2 lround(-2.5) = -3 lround(-2.7) = -3 llround(-0.01234) = 0 llround(2.3563) = 2 llround(2.555) = 3 llround(2.7896) = 3 llround(-2.323) = -2 llround(-2.5258) = -3 llround(-2.71236) = -3
Here, in the above program we have just calculated the nearest integral value of given float or double value.
which has been calculated accurately.
- 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 .
// C+++ code for above explanation
// Driver program
// Initializing values for int type
a1 = 25, b1 = 30;
// Initializing values for double type
a2 = .25, b2 = .30;
ans_1 = (a1 * b1);
ans_2 = (a2 * b2);
// Rounded result for both
"From first multiplication :"
<< round(ans_1) <<
"From second multiplication :"
<< round(ans_2) <<
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.
Reference : www.mathworksheetcenter.com, www.cplusplus.com
This article is contributed by Himanshu Ranjan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Left-Right traversal of all the levels of N-ary tree
- Difference between Iterators and Pointers in C/C++ with Examples
- ostream::seekp(pos) method in C++ with Exmaples
- Default Methods in C++ with Examples
- C++ Tutorial
- Hello World Program : First program while learning Programming
- Difference between Argument and Parameter in C/C++ with Examples
- <cfloat> float.h in C/C++ with Examples
- C/C++ #include directive with Examples
- C/C++ if else statement with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.