Ratio_greater () function in C++


Given is the task to show the working of ratio_greater () function in c++.

The given function Ratio_greater checks if the value of ratio1 is greater than ratio2. It returns a Boolean constant “value” which returns true if ratio1 is greater than ratio2 else returns false.

Syntax

Template <ratio1, ratio2> ratio_greater

Parameters

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

Explanation of this function

In this function, if the value of ratio1 is greater 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.

Example

Input: 1/3 and 3/9
Output: 3/9 is greater than 1/3.
Input: 4/16 and 1/3
Output: 4/16 is greater than 1/3.

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 greater than the value of ratio2.

  • Using ratio_greater we can check that

Example

// C++ code to demonstrate the working of ratio_greater
#include<iostream>
#include<ratio>
using namespace std;
int main( ){
   // Declaring ratios
   typedef ratio<1, 2> ratio1;
   typedef ratio<1, 4> ratio2;
   // Checking ratio1 is greater than ratio2.
   if (ratio_greater<ratio1, ratio2>: : value )
      cout<< “ ratio1 is greater than ratio2”;
   else
      cout<< “ ratio1 is not greater than ratio2”;
   cout<< “ endl”;
   return 0;
}

Output

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

1/2 is greater than 1/4.
1/3 is not greater than 1/2.

Updated on: 28-Feb-2020

22 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements