Ratio Manipulations in C++ | Set 2 (Comparison)
Last Updated :
17 Aug, 2023
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
Share your thoughts in the comments
Please Login to comment...