ratio_not_equal() in C++ with examples

C++Server Side ProgrammingProgramming

In this article, we will be discussing the working, syntax, and examples of ratio_not_equal template in C++ STL.

What is ratio_not_equal template?

ratio_not_equal template is inbuilt in C++ STL, which is defined in <ratio> header file. ratio_not_equal is used to compare the two ratios that are unequal. This template accepts two parameters and check whether the given ratios should not be equal. Like we have two ratios, 1/2 and 3/9 which are not equal so it stands true for the given template. This function returns true when the two ratios are unequal.

So, when we want to check for the inequality 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_not_equal;

Parameters

The template accepts the following parameter(s) −

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

Return value

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

Input

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

Output

false

Input

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

Output

true

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_not_equal<R_1, R_2>::value)
cout<<"Ratio 1 and Ratio 2 aren't equal";
else
cout<<"Ratio 1 and Ratio 2 are 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

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

Output

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

Ratio 1 and Ratio 2 aren equal
Updated on 22-Apr-2020 12:29:20