ratio_less_equal() function in C++
An intrinsic C++ function called ratio_less_equal() determines whether the ratios R1 and R2 are equal or less than one another. If the ratio is less than or equal to ratio 2, it returns True; otherwise, it returns False.
Syntax:
It has the following syntax:
template < class ratio1_name, class ratio2_name > ratio_less_equal
Template parameters: Two template parameters, ratio1 and ratio2, are accepted by the function and are to be compared.
Value returned: The function returns a boolean value, returning false otherwise and true if the ratios are less than or equal to each other.
C++ Code:
Let's take an example to illustrate the use of the ratio_less_equal() in C++.
#include <iostream>
#include <ratio>
template <typename R1, typename R2>
constexpr bool ratio_less_equal() {
return R1::num * R2::den <= R2::num * R1::den;
}
int main() {
using Ratio1 = std::ratio<1, 2>; // 1/2
using Ratio2 = std::ratio<2, 4>; // 2/4 is equivalent to 1/2
if (ratio_less_equal<Ratio1, Ratio2>()) {
std::cout << "Ratio1 is less than or equal to Ratio2." << std::endl;
} else {
std::cout << "Ratio1 is greater than Ratio2." << std::endl;
}
return 0;
}
Output:
Ratio1 is less than or equal to Ratio2.
Example 2:
Let's take another example to illustrate the use of the ratio_less_equal() in C++.
#include <iostream>
#include <ratio>
template <typename R1, typename R2>
constexpr bool ratio_less_equal() {
return R1::num * R2::den <= R2::num * R1::den;
}
int main() {
using Ratio1 = std::ratio<2, 3>; // 2/3
using Ratio2 = std::ratio<4, 5>; // 4/5
if (ratio_less_equal<Ratio1, Ratio2>()) {
std::cout << "Ratio1 is less than or equal to Ratio2." << std::endl;
} else {
std::cout << "Ratio1 is greater than Ratio2." << std::endl;
}
return 0;
}
Output:
Ratio1 is less than or equal to Ratio2.
EXAMPLE 3:
Let's take another example to illustrate the use of the ratio_less_equal() in C++.
#include <iostream>
#include <ratio>
using namespace std;
int main()
{
typedef ratio<2, 5> ratio1; // Change: 2/5
typedef ratio<3, 7> ratio2; // Change: 3/7
// check if R1 <= R2
if (ratio_less_equal<ratio1, ratio2>::value)
cout << "2/5 is less than or equal to 3/7";
else
cout << "2/5 is greater than 3/7";
return 0;
}
Output:
2/5 is less than or equal to 3/7
Example 4:
#include <iostream>
#include <ratio>
int main() {
// Define two ratios
typedef std::ratio<4, 9> ratio1; // Change: 4/9
typedef std::ratio<5, 11> ratio2; // Change: 5/11
// Check if ratio1 <= ratio2
if (std::ratio_less_equal<ratio1, ratio2>::value)
std::cout << "4/9 is less than or equal to 5/11" << std::endl;
else
std::cout << "4/9 is greater than 5/11" << std::endl;
return 0;
}
Output:
4/9 is less than or equal to 5/11
Explanation:
4/9 is the value of ratio1, and 5/11 is the value of ratio2. When determining whether ratio1 is less than or equal to ratio2, the program uses std::ratio_less_equal. The result is printed appropriately. You can change the denominator and numerator values in ratios 1 and 2 to see distinct results depending on the comparison.
Example 5:
// C++ program to illustrate the ratio_less_equal function
#include <iostream>
#include <ratio>
using namespace std;
int main()
{
typedef ratio<3, 15> ratio1; // Change: 3/15 is equivalent to 1/5
typedef ratio<2, 9> ratio2; // Change: 2/9
// check if R1 <= R2
if (ratio_less_equal<ratio1, ratio2>::value)
cout << "3/15 is less than or equal to 2/9";
else
cout << "3/15 is greater than 2/9";
return 0;
}
Output:
3/15 is less than or equal to 2/9
Example 6:
// C++ program to illustrate the
// ratio_less_equal function
#include <iostream>
#include <ratio>
using namespace std;
int main()
{
typedef ratio<7, 15> ratio1; // Change: 7/15
typedef ratio<2, 5> ratio2; // Change: 2/5
// check if R1 <= R2
if (ratio_less_equal<ratio1, ratio2>::value)
cout << "7/15 is less than or equal to 2/5";
else
cout << "7/15 is greater than 2/5";
return 0;
}
Output:
7/15 is greater than 2/5
Conclusion:
It can be implemented as a custom template function to compare two std::ratio types. Using two ratio types as template parameters, this function determines whether the ratio represented by the first type is smaller than or equal to the ratio represented by the second type. By calculating the product of the numerators and denominators of the two ratios, the function compares the two. By entering distinct combinations for the numerator and denominator, users can alter the ratio values and see how the function performs in different situations. It's crucial to remember that this function is usually defined in user code or particular libraries; the standard C++ library may not contain it.