ratio_greater() function in C++

In this article, you will learn about the ratio_greater() function in C++ with its syntax, parameters, and examples.

The ratio_greater() is a built-in C++ function determining whether ratio R1 is more significant than ratio R2. The Boolean constant "value" is returned; if ratio 1 is more significant than ratio 2, it returns true; otherwise, it returns false.

Syntax:

It has the following syntax:

Parameters:

  1. template <class ratio1_name, class ratio2_name>: A template with the type parameters ratio1_name and ratio2_name is declared in this line. Here, the keyword class denotes that they are type parameters. As type name and class are frequently used interchangeably in template declarations, you may also use type name in place of class.
  2. ratio_greater: It is the template's name. In C++, generic functions and classes can be defined using templates; in this instance, ratio_greater seems to be the name of a template function.
  3. ratio1_name and ratio2_name: The types that will be utilized when the template is instantiated are represented by the template parameters ratio1_name and ratio2_name. They serve as stand-ins for the fundamental kinds available if the template is used. These options appear to be anticipated to indicate types associated with ratios in the context of ratio_greater, maybe as template arguments.

Example 1:

Let us take an example to illustrate the ratio_greater() function in C++.

Output:

Is ratio1 greater than ratio2? false
Is ratio3 greater than ratio4? true

Explanation:

1. Header includes

  • #include <iostream>: This header supports the input and output operations.
  • #include <ratio>: This header presents the std::ratio template class, a compile-time rational constant representation.

2. Definition of a function template

  • template<class Ratio1, class Ratio2>: A function template called ratio_greater is declared in this line. Ratio1 and Ratio2 are the two types of parameters required.
  • bool ratio_greater() constexpr {... }: This function template can be evaluated at compilation time because it can return a bool and is tagged constexpr. There is only one return statement in the body of the function.

3. Function body

return a ratio of 1::num * 2::den > a ratio of 2::num * 1::den;

  • It is the body of the function, containing a single return statement.
  • The expression compares the magnitudes of two ratios (Ratio1 and Ratio2) by cross-multiplying their numerators and denominators.
  • If the numerator of Ratio1 multiplied by the denominator of Ratio2 is greater than the numerator of Ratio2 multiplied by the denominator of Ratio1, the function returns true. Otherwise, it returns false.
  • The comparison result is a boolean value representing whether Ratio1 is greater than Ratio2 in magnitude.

4. main() function

  • int main() {... }: It is where the program starts.
  • ratio1 and ratio2 are instances of std::ratio<1, 2> and std::ratio<2, 3>. The ratio_greater function is called with these ratios, and the result is printed.
  • Ratio 3 and Ratio 4 are two distinct ratio types, similar to Example 1. return 0; shows that the program has been run successfully.

The program shows how to use the ratio_greater template function to compare two sets of ratios. After that, it outputs if the first ratio in each pair is larger than the second. Boolean values can be printed as "true" or "false" for more effortless reading using the std::boolalpha manipulator.

Example 2:

Let us take another example to illustrate the ratio_greater() function in C++.

Output:

10/20 is not more than 42/8

Explanation:

  1. Header includes
    • #include <iostream>: If offers capabilities for operations involving input and output.
    • #include <ratio>: It includes the definition of the std::ratio template class in the <ratio> header.
  2. Type Definitions
    • ratio of typedef<10, 20> ratio1;: Using the std::ratio template, defines a type alias ratio1 for the ratio 10/20.
    • ratio typedef <42, 8> ratio2;: Using the std::ratio template, defines a type alias ratio2 for the ratio 42/8.
  3. Using ratio_greater for comparison
    • The code makes an effort to use a hypothetical ratio_greater function template to determine whether ratio1 is greater than ratio2. The code snippet does not define ratio_greater. Therefore, it would typically result in a compilation error.
  4. Return Statement
    • return 0;: It denotes that the program has been executed successfully.

Conclusion:

In conclusion, the code defines two ratios (ratio1 and ratio2) and tries to compare them using a fictitious ratio_greater function template. Afterwards, a message is printed according to the comparison's outcome.






Latest Courses