ratio_equal() and ratio_not_equal() in C++

In this tutorial, you will learn about the two ratio manipulation functions in C++ i.e. ratio_equal() and ratio_not_equal(). Apart from these two ratio manipulation functions, there are other functions also for adding, subtracting, multiply and divide ratios. But, in this tutorial, we will discuss only two of them. It returns only boolean member constant “value“.

What is ratio_equal() and ratio_not_equal() ?

These are two ratio manipulation functions that are used to check whether the given ratios are equal or not. As the function name itself suggests ratio_equal() accepts value only if the ratio is equal. If it does not receive any value that means the ratio is not equal. And similarly, ratio_not_equal() accepts value only of the ratio is not equal. Now let us discuss how to implement them in C++.

Note: Before learning the implementation of functions in C++ you must note that you have to import the header file “ratio“.

#include<ratio>

Defining a ratio:-

typedef ratio<5,10> ratio1;
typedef ratio<1, 2> ratio2;

Using function ratio_not_equal:-

if (ratio_not_equal<r1, r2>::value) {
        cout << "Ratio is not equal";
    }
    else {
        cout << "Ratio is equal";
    }
}

Output:-

Ratio is equal

Using function ratio_equal:-

if (ratio_equal<r1, r2>::value) {
        cout << "Ratio is equal";
    }
    else {
        cout << "Ratio is not equal";
    }
}

Output:-

Ratio is equal

Conclusion

So, now you know how to declare ratio and how to use ratio manipulation functions, ratio_equal() and ratio_not_equal(). Apart from arithmetical operations like addition, subtraction, multiplication, and division. you can also check which ratio is greater or less or greater than equal to and less than equal to. So you must try them also.

Leave a Reply

Your email address will not be published. Required fields are marked *