Related Tags

c++
communitycreator

How to use the ratio_less_equal() function in C++

Harsh Jain

In this shot, we will learn how to use the ratio_less_equal() function. This template alias is used to check whether the first ratio object is less than or equal to the second ratio object or not. It gives the Boolean value as a result i.e., either true or false. The comparison is done between the simplest form of the two ratios by the ratio_less_equal() method.

The ratio_less_equal() function is available in <ratio> header file in C++.

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 the examples:

• Suppose that the first ratio is 1 : 5 and the second ratio is 1 : 2.
• The result of the ratio_less_equal() function for comparison gives the result as true.

Let’s explore how the less than or equal to check took place:

• The ratios 1 : 5 and 1 : 2 can be represented as $\frac{1}{5}$ and $\frac{1}{2}$ in fractional form respectively.
• Both the ratios are already simplified.
• Now, the simplified fractions i.e., $\frac{1}{5}$ and $\frac{1}{2}$ are compared.

Hence, we can see that the first ratio is less than or equal to the second ratio and we get the final result is true.

Parameter

The ratio_less_equal() method takes the following parameters:

• Ratio1: A ratio object which is to be compared for less than or equal to inequality with the other ratio object.
• Ratio2: Another ratio object which gets compared for less than or equal to inequality with the previous ratio object.

Return value

This function returns the Boolean result after comparison for less than or equal to.

• If the first ratio object is less than or equal to second ratio object, then it returns true.

• If the first ratio object is not less than or equal to second ratio object, then it returns false.

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<1, 5> ratio2;

if (ratio_less_equal<ratio1, ratio2>::value)
cout<<"Ratio 1 is less than or equal to Ratio 2";
else
cout<<"Ratio 1 is not less than or equal to Ratio 2";

return 0;
}

Use ratio_less_equal() function in C++

Explanation

• In lines 1 and 2, we imported the required header files.

• In line 5, we made a main() function.

• In lines 7 and 8, we declared the two ratios.

• In line 10, we performed the comparison between the two declared ratios by using ratio_less_equal() function as a condition inside the if statement. If the first ratio object is less than or equal to the second ratio object, then the if statement gets executed and displays the message regarding the result.

• In line 12, if the condition doesn’t satisfy i.e., the first ratio object is neither less than nor equal to the second ratio object, then the else statement gets executed and displays the message regarding the result.

In this way, we can use the ratio_less_equal() function to check whether the first ratio object is less than or equal to the second ratio object.

RELATED TAGS

c++
communitycreator

CONTRIBUTOR

Harsh Jain
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time