In this shot, we learn how to use the ratio_not_equal()
function.
This template alias is used to check the equality between two ratios and give the boolean value as a result. It returns true if the two ratios are not equal, or false if the two ratios are equal.
The ration_not_equal()
method does this comparison between the simplest form of the two ratios.
The
ratio_not_equal()
function is available in the<ratio>
header file in C++.
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 some examples. Suppose we have the following ratios:
The result of the ratio_not_equal()
function on comparison for equality of these two ratios gives the result as true.
Let’s explore how the equality check took place.
The ratios 2 : 3 and 3 : 6 can be represented as $\frac{2}{3}$ and $\frac{3}{6}$ in fractional form respectively.
The ratio $\frac{2}{3}$ is itself a simplified form. The ratio $\frac{3}{6}$ on simplification gives $\frac{1}{2}$.
Now, the simplified fractions $\frac{2}{3}$ and $\frac{1}{2}$ are compared for equality. Since the two fractions are not equal, the final result is true
.
The ratio_not_equal()
method takes the parameters below:
It returns a boolean result after comparison for equality.
If the ratio objects are not equal then it returns true
.
If the ratio objects are equal then it returns false
.
Let’s take a look at the code.
#include <iostream> #include <ratio> using namespace std; int main() { typedef ratio<2, 3> ratio_one; typedef ratio<3, 6> ratio_two; if (ratio_not_equal<ratio_one, ratio_two>::value) cout<<"The ratio_one and ratio_two are not equal"; else cout<<"The ratio_one and ratio_two are equal"; return 0; }
main()
function.ratio_not_equal()
function as a condition inside the if statement. If the two ratio objects are not equal, then the if statement gets executed and displays the message regarding the result.In this way, we can use the ratio_not_equal()
function to check whether the two ratios are equal.
RELATED TAGS
CONTRIBUTOR
View all Courses