In this shot, we will learn how to use the ratio_greater_equal()
function. The ratio_greater_equal()
function is available in the <ratio>
header file in C++. ratio_greater_equal()
is used to check whether the first ratio object is greater than or equal to the second ratio object and gives the boolean value as a result, i.e., true or false. The ratio_greater_equal()
method compares the simplest form of the two ratios.
A ratio is a representation of a fraction in which the numerator and denominator are differentiated by the colon (:) symbol. The numerator and denominator can be of float
data type.
Let’s understand with the help of some examples:
Suppose that the first ratio is and the second ratio is .
The result of the ratio_greater_equal()
function is true
.
The ratios and can be represented as and in fractional form, respectively.
The ratio is itself a simplified form. The simplified form of is .
Now, we compare the simplified fractions, and . We can see that the first ratio is greater than the second ratio, and we get the final result of true
.
The ratio_greater_equal()
method takes two parameters:
Ratio1
: A ratio
object to be compared with the other ratio
object for greater than or equal to inequality.Ratio2
: Another ratio
object that gets compared for greater than or equal to inequality with the previous ratio
object.ratio_greater_equal()
returns the boolean result after comparison for greater than or equal to.
If the first ratio
object is greater than or equal to the second ratio
object, then the function returns true
.
If the first ratio
object is not greater than or equal to the second ratio
object, then the function returns false
.
Let’s have a look at the code.
#include <iostream>#include <ratio>using namespace std;int main(){typedef ratio<1, 2> ratio1;typedef ratio<5, 25> ratio2;if (ratio_greater_equal<ratio1, ratio2>::value)cout<<"Ratio 1 is greater than or equal to Ratio 2";elsecout<<"Ratio 1 is smaller than or equal to Ratio 2";return 0;}
In lines 1 and 2, we import the required header files.
In line 5, we create a main()
function.
In lines 7 and 8, we declare two ratios. Here, we use typedef
to assign an alternative name to a datatype.
In line 10, we use the ratio_greater_equal()
function as a condition in the if
statement to perform the comparison between the two declared ratios. If the first ratio
object is greater than or equal to the second ratio
object, the if
statement is executed and displays the message with the result. Here, the value
variable stores the boolean result of the greater than or equal to inequality check.
In line 12, if the condition isn’t satisfied, i.e., the first ratio object is neither greater than nor equal to the second ratio object, then the else
statement is executed and displays the result.
We can use the ratio_greater_equal()
function to check whether the first ratio
object is greater than or equal to the second ratio
object.