ratio_equal() in C++ with example


In this article, we will be discussing the working, syntax, and examples of ratio_equal() function in C++ STL.

What is ratio_equal template?

ratio_equal template is inbuilt in C++ STL, which is defined in <ratio> header file. ratio_equal is used to compare the two ratios. This template accepts two parameters and checks whether the given ratios are equal or not. Like we have two ratios, 1/2 and 3/6 which are equal when we simplify them but the numbers are not equal so C++ has an inbuilt template to check whether the two ratios are equal or not and return true if they are equal else false.

So, when we want to check for the equality of the two ratios, instead of writing whole logic in C++ we can use the provided template which makes the coding easier.

Syntax

template <class ratio1, class ratio2> ratio_equal;

Parameters

The template accepts the following parameter(s) −

  • ratio1, ratio2 − These are the two ratios that we want to check they are equal or not.

Return value

This function returns true when the two ratios are equal else the function returns false.

Input 

typedef ratio<3, 6> ratio1;
typedef ratio<1, 2> ratio2;
ratio_equal<ratio1, ratio2>::value;

Output 

true

Input 

typedef ratio<3, 9> ratio1;
typedef ratio<1, 2>ratio2;
ratio_equal<ratio1, ratio2>::value;

Output 

false

Example

 Live Demo

#include <iostream>
#include <ratio>
using namespace std;
int main(){
   typedef ratio<2, 5> R_1;
   typedef ratio<10, 25> R_2;
   //check whether ratios are equal or not
   if (ratio_equal<R_1, R_2>::value)
      cout<<"Ratio 1 and Ratio 2 are equal";
   else
      cout<<"Ratio 1 and Ratio 2 aren't equal";
   return 0;
}

Output

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

Ratio 1 and Ratio 2 are equal

Example

 Live Demo

#include <iostream>
#include <ratio>
using namespace std;
int main(){
   typedef ratio<2, 5> R_1;
   typedef ratio<1, 3> R_2;
   //check whether ratios are equal or not
   if (ratio_equal<R_1, R_2>::value)
      cout<<"Ratio 1 and Ratio 2 are equal";
   else
      cout<<"Ratio 1 and Ratio 2 aren't equal";
   return 0;
}

Output

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

Ratio 1 and Ratio 2 aren’t equal

Example

 Live Demo

Code-3:
//if we try to enter 0 in the denominator then the output will be
#include <iostream>
#include <ratio>
using namespace std;
int main(){
   typedef ratio<2, 5> R_1;
   typedef ratio<1, 0> R_2;
   //check whether ratios are equal or not
   if (ratio_equal<R_1, R_2>::value)
      cout<<"Ratio 1 and Ratio 2 are equal";
   else
      cout<<"Ratio 1 and Ratio 2 aren't equal";
   return 0;
}

Output

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

/usr/include/c++/6/ratio:265:7: error: static assertion failed: denominator cannot be zero
static_assert(_Den != 0, "denominator cannot be zero");

Updated on: 22-Apr-2020

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements