Ratio_less () function in C++


Given is the task to show the working of ratio_less () function in C++.

The function ratio_less() checks if the value of ratio1 is less than ratio2. It returns a Boolean constant “value” which returns true if ratio1 is less than ratio2 else returns false.

Example

Input: 1/3 and 3/9
Output: 1/3 is less than 3/9.
Input: 1/4 and 1/8
Output: 1/4 is not less than 1/8.

Syntax

Template <ratio1, ratio2> ratio_less

Parameters

This function accepts two template parameters one is ratio1 and another one is ratio2 which are to be compared.

Explanation

In this function, if the value of ratio1 is less than the value of ratio2 then this function will return the Boolean value which is true i.e. integer digit 1 otherwise it will return false i.e. integer digit 0.

Explanation of typedef: Typedef is used to give data type a new name, in this program we use typedef to declare the ratios. Typedef creates the alias that can be used anywhere in place of a type name. It may declare one or many identifiers on the same line and also it can be used to declare an array and function types, pointers, references, class types, etc.

Approach that we are using in the below program

  • First we declare the two ratios.

  • Then assign the values of two ratios.

  • Then we check if the value of ratio1 is less than the value of ratio2.

  • Using ratio_less we can check that

Example

// C++ code to demonstrate the working of ratio_less
#include<iostream>
#include<ratio>
Using namespace std;
int main( ){
   typedef ratio<1, 3> ratio1;
   typedef ratio<1, 2> ratio2;
   if(ratio_less<ratio1, ratio2>: : value)
      cout<< “ ratio1 is less than ratio2”;
   else
      cout<< “ ratio1 is not less than ratio2”;
   return 0;
}

Output

If we run the above code then it will generate the following output

1/3 is less than 1/2.
1/3 is not less than 1/5.

Updated on: 28-Feb-2020

23 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements