How to use the ratio_greater_equal() function in C++
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.
What is a ratio?
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 istrue. -
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.
Parameters
The ratio_greater_equal() method takes two parameters:
Ratio1: Aratioobject to be compared with the otherratioobject for greater than or equal to inequality.Ratio2: Anotherratioobject that gets compared for greater than or equal to inequality with the previousratioobject.
Return value
ratio_greater_equal() returns the boolean result after comparison for greater than or equal to.
-
If the first
ratioobject is greater than or equal to the secondratioobject, then the function returnstrue. -
If the first
ratioobject is not greater than or equal to the secondratioobject, then the function returnsfalse.
Code
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;}
Explanation
-
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
typedefto assign an alternative name to a datatype. -
In line 10, we use the
ratio_greater_equal()function as a condition in theifstatement to perform the comparison between the two declared ratios. If the firstratioobject is greater than or equal to the secondratioobject, theifstatement is executed and displays the message with the result. Here, thevaluevariable 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
elsestatement 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.