In this shot, we will learn how to use the ratio_divide()
function.
The ratio_divide()
function is available in the <ratio>
header file in C++. This template alias is used to divide the two ratios.
A ratio is a representation of a fraction in which the numerator and denominator are differentiated by a colon (:) symbol. The numerator and denominator can be of the float
data type.
Let’s understand with the help of an example. Suppose that the first ratio is 10 : 3 and the second ratio is 5 : 2.
So, the result of the ratio_divide()
function on these two ratios gives the result of 4 : 3. Let’s dive into how the division took place.
The ratio 10 : 3 and 5 : 2 can be represented as and in the fractional form, respectively.
Now, the division = / = =
Now, after simplification of we get . So, this is the final result of division.
The ratio_divide()
method takes the following parameters:
ratio
object which behaves as a dividend in the division.ratio
object which behaves as a divisor for the dividend above in the division.It returns the result of the division in the simplest form. It returns two member constants:
num
: The simplified numerator of the ratio after the division of the two ratios.den
: The simplified denominator of the ratio after the division of the two ratios.Let’s have a look at the code.
#include <iostream> #include <ratio> using namespace std; int main() { typedef ratio<10, 3> r1; typedef ratio<5, 2> r2; typedef ratio_divide< r1, r2 > division; cout << "The ratio after division is : "; cout << division::num << "/" << division::den; cout << endl; return 0; }
main()
function.ratio_divide()
function.division
variable and displayed the result.So, this is how to use the ratio_divide()
function to divide the two ratios and get the division in the simplest form.
RELATED TAGS
CONTRIBUTOR
View all Courses