Skip to content
Related Articles

Related Articles

Ratio Manipulations in C++ | Set 2 (Comparison)

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 18 Jan, 2022

Prerequisite Ratio Manipulations in C++ | Set 1(Arithmetic)

In C++, the <ratio> header file allows us to manipulate ratios using various inbuilt template alias. The header file was introduced from C++11 onwards. In this article, we will be discussing the Comparison of Ratio Manipulations in C++. The following functions are used:

  • ratio_equal
  • ratio_not_equal
  • ratio_greater
  • ratio_less
  • ratio_greater_equal
  • ratio_less_equal

1. ratio_equal: This template alias checks if the ratios in its arguments are equal. Returns true if equal, else returns false. It returns a boolean member constant “value”

2. ratio_not_equal: This template alias checks if the ratios in its arguments are not equal. Returns true if not equal, else if equal returns false. It returns a boolean member constant “value”.  

CPP




// C++ code to demonstrate the working of
// ratio_equal and ratio_not_equal
#include <iostream>
#include <ratio> // for ratio manipulation
using namespace std;
 
// Driver Code
int main()
{
    // Declaring ratios
    typedef ratio<10, 100> ratio1;
    typedef ratio<1, 10> ratio2;
 
    // Checking if ratios are equal using ratio_equal
    ratio_equal<ratio1, ratio2>::value
        ? cout << "Ratios are equal"
        : cout << "Ratios are not equal";
    cout << endl;
 
    // Checking if ratios are not equal using
    // ratio_not_equal
    ratio_not_equal<ratio1, ratio2>::value
        ? cout << "Ratios are not equal"
        : cout << "Ratios are equal";
 
    return 0;
}

Output

Ratios are equal
Ratios are equal

3. ratio_greater: This temporary alias checks if ratio1 is greater than ratio2. It returns a boolean member constant “value” which returns true if ratio1 is greater than ratio2 else returns false.

4. ratio_less: This temporary alias checks if ratio1 is less than ratio2. It returns a boolean member constant “value” which returns true if ratio1 is less than ratio2 else returns false. 

CPP




// C++ code to demonstrate the working of
// ratio_greater and ratio_less
#include <iostream>
#include <ratio> // for ratio manipulation
using namespace std;
 
// Driver Code
int main()
{
    // Declaring ratios
    typedef ratio<10, 100> ratio1;
    typedef ratio<11, 100> ratio2;
 
    // Checking if ratio1 is greater than ratio2
    // using ratio_greater
    ratio_greater<ratio1, ratio2>::value
        ? cout << "ratio1 is greater than ratio2"
        : cout << "ratio1 is not greater than ratio2";
    cout << endl;
 
    // Checking if ratio1 is less than ratio2
    // using ratio_less
    ratio_less<ratio1, ratio2>::value
        ? cout << "ratio1 is less than ratio2"
        : cout << "ratio1 is not less than ratio2";
    cout << endl;
 
    return 0;
}

Output

ratio1 is not greater than ratio2
ratio1 is less than ratio2

5. ratio_greater_equal: This temporary alias checks if ratio1 is greater or equal than ratio2. It returns a boolean member constant “value” which returns true if ratio1 is greater or equal than ratio2 else returns false.

6. ratio_less_equal: This temporary alias checks if ratio1 is less or equal than ratio2. It returns a boolean member constant “value” which returns true if ratio1 is less or equal than ratio2 else returns false. 

CPP




// C++ code to demonstrate the working of
// ratio_greater_equal and ratio_less_equal
#include <iostream>
#include <ratio> // for ratio manipulation
using namespace std;
 
// Driver Code
int main()
{
    // Declaring ratios
    typedef ratio<10, 100> ratio1;
    typedef ratio<1, 10> ratio2;
 
    // Checking if ratio1 is greater or equal than ratio2
    // using ratio_greater_equal
    ratio_greater_equal<ratio1, ratio2>::value
        ? cout << "ratio1 is greater or equal than ratio2"
        : cout << "ratio1 is not greater or equal than "
                  "ratio2";
    cout << endl;
 
    // Checking if ratio1 is less or equal than ratio2
    // using ratio_less_equal
    ratio_less_equal<ratio1, ratio2>::value
        ? cout << "ratio1 is less or equal than ratio2"
        : cout << "ratio1 is not less or equal than ratio2";
    cout << endl;
 
    return 0;
}

Output

ratio1 is greater or equal than ratio2
ratio1 is less or equal than ratio2

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!