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
#include <iostream>
#include <ratio> // for ratio manipulation
using namespace std;
int main()
{
typedef ratio<10, 100> ratio1;
typedef ratio<1, 10> ratio2;
ratio_equal<ratio1, ratio2>::value
? cout << "Ratios are equal"
: cout << "Ratios are not equal" ;
cout << endl;
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
#include <iostream>
#include <ratio> // for ratio manipulation
using namespace std;
int main()
{
typedef ratio<10, 100> ratio1;
typedef ratio<11, 100> ratio2;
ratio_greater<ratio1, ratio2>::value
? cout << "ratio1 is greater than ratio2"
: cout << "ratio1 is not greater than ratio2" ;
cout << endl;
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
#include <iostream>
#include <ratio> // for ratio manipulation
using namespace std;
int main()
{
typedef ratio<10, 100> ratio1;
typedef ratio<1, 10> ratio2;
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;
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
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Aug, 2023
Like Article
Save Article