Ratio Manipulations in C++ | Set 1 (Arithmetic) - GeeksforGeeks

18 Jan, 2022
C++ allows us to perform addition, subtraction, multiplication, and division operations on fractions. One method of adding ratios is discussed in the following article – Program to add two fractions. The method used here is tedious and lengthy, so to overcome that a better method was introduced in C++. The <ratio> header file in C++ 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 Arithmetic Ratio Manipulations in C++. The following functions are used:
ratio_add
ratio_subtract
ratio_multiply
ratio_divide
1) ratio_add: This template alias is used to add two ratios and return the result in the simplest form. It returns two-member constants, num, and den denoting numerator and denominator.
2) ratio_subtract: This template alias is used to subtract two ratios and return the result in the simplest form. It returns two-member constants, num, and den denoting numerator and denominator. It subtracts ratio2 from ratio1. 
CPP
// C++ program to demonstrate the working of// ratio_add and ratio_subtract#include <iostream>#include <ratio> // for ratio manipulationusing namespace std;int main(){    // Declaring ratios    typedef ratio<5, 4> ratio1;    typedef ratio<3, 4> ratio2;     // Summing two ratios    typedef ratio_add<ratio1, ratio2> sum;     // Subtracting two ratios    typedef ratio_subtract<ratio1, ratio2> diff;     // printing sum of ratios    cout << "The sum of ratios is : ";    cout << sum::num << "/" << sum::den;    cout << endl;     // printing difference of ratios    cout << "The difference of ratios is : ";    cout << diff::num << "/" << diff::den;    cout << endl;     return 0;}
The sum of ratios is : 2/1
The difference of ratios is : 1/2
3. ratio_multiply: This template alias is used to multiply two ratios and return the result in the simplest form. It returns two-member constants, num, and den denoting numerator and denominator.
4. ratio_divide: This template alias is used to divide two ratios and return the result in the simplest form. It returns two-member constants, num, and den denoting numerator and denominator. It divides ratio1 by ratio2. 
CPP
// C++ program to demonstrate the working of// ratio_multiply and ratio_divide#include <iostream>#include <ratio> // for ratio manipulationusing namespace std;int main(){    // Declaring ratios    typedef ratio<5, 4> ratio1;    typedef ratio<3, 4> ratio2;     // Multiplying two ratios    typedef ratio_multiply<ratio1, ratio2> prod;     // Dividing two ratios    typedef ratio_divide<ratio1, ratio2> div;     // printing product of ratios    cout << "The product of ratios is : ";    cout << prod::num << "/" << prod::den;    cout << endl;     // printing division of ratios    cout << "The division of ratios is : ";    cout << div::num << "/" << div::den;    cout << endl;     return 0;}
The product of ratios is : 15/16
The division of ratios is : 5/3
For Next Set Refer to this article:
Ratio Manipulations in C++ | Set 2 (Comparison)
This article is contributed by Manjeet Singh. 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. 
akshaysingh98088
anshikajain26
CPP-Library
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Left Shift and Right Shift Operators in C/C++
Function Pointer in C
Core Dump (Segmentation fault) in C/C++
rand() and srand() in C/C++
Vector in C++ STL
Inheritance in C++
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
C++ Classes and Objects