Open In App

C++ 11 – <ratio> Header

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The header file, brought about by C++11, offers a class template for ratios and a collection of aliases to represent the same. The purpose of this is to enable the definition and manipulation of ratios while also providing a means to express the links between diverse units of measurement.

Ratio Header in C++

The Ratio class is a template class that is used to represent ratios. It is defined as follows:

template <intmax_t Num, intmax_t Den = 1>
class ratio;

The ratio can be defined using the Num and Den parameters, where Num stands for the numerator and Den stands for the denominator. These parameters are whole numbers and Den is assigned a default value of 1.

Examples of Ratio Header

Example 1:

C++




// C++ Program to demonstrate the use of the <ratio> Header
#include <iostream>
#include <ratio>
  
int main()
{
    typedef std::ratio<3, 5> MyRatio;
    std::cout << "MyRatio numerator: " << MyRatio::num
              << std::endl;
    std::cout << "MyRatio denominator: " << MyRatio::den
              << std::endl;
    return 0;
}


Output

MyRatio numerator: 3
MyRatio denominator: 5

Explanation

In this example, A novel ratio termed MyRatio is created by assigning a numerator of 3 and a denominator of 5. The values of the numerator and denominator are printed using the MyRatio class’ static members num and den.

Type aliases

using atto  = ratio<1, 1000000000000000000LL>;
using femto = ratio<1, 1000000000000000LL>;
using pico  = ratio<1, 1000000000000LL>;
using nano  = ratio<1, 1000000000>;
using micro = ratio<1, 1000000>;
using milli = ratio<1, 1000>;
using centi = ratio<1, 100>;
using deci  = ratio<1, 10>;
using deca  = ratio<10, 1>;
using hecto = ratio<100, 1>;
using kilo  = ratio<1000, 1>;
using mega  = ratio<1000000, 1>;
using giga  = ratio<1000000000, 1>;
using tera  = ratio<1000000000000LL, 1>;
using peta  = ratio<1000000000000000LL, 1>;
using exa   = ratio<1000000000000000000LL, 1>;

Example 2:

C++




// C++ Program to demonstrate the type aliases to define a
// ratio
#include <iostream>
#include <ratio>
  
int main()
{
    typedef std::ratio<5, std::mega::den> FiveMegabytes;
    std::cout << "Five megabytes is " << FiveMegabytes::num
              << " " << std::mega::symbol << "bytes."
              << std::endl;
    return 0;
}


Output: 

Five megabytes is 5 MB bytes.

Advantages of using <ratio> Header

The advantages of ratio header are mentioned below:

  • Improved readability: Ratios could be utilized to comprehend the numerical connections among values in coding. They offer an alternative approach to representing numerical values that enhances clarity in conveying their intended purpose.
  • Simplified conversions: Converting between different units of measure becomes a simple task with the help of ratios. With bytes as the base value, one can easily convert it to kilobytes or megabytes with the appropriate ratios. This not only simplifies your code but also ensures easier maintenance.
  • Type safety: Type-checking can be done by the compiler on ratios that are considered types. With this method, errors can be identified during compile time instead of waiting for runtime.
  • Improved precision: Numerical values that demand high precision can be expressed without any loss of accuracy through ratios. This feature of ratios plays a vital role in certain applications.
  • Standardized interface: The use of the header facilitates a uniform and consistent means of dealing with ratios. Such an approach simplifies the process of developing software that can be easily transferred between various operating systems and software development environments.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads