What is C++ std::ratio?

C++ is a programming language that often surprises programmers with the depth and breadth of its standard library. Among the less talked about yet incredibly useful components of the C++ Standard Template Library (STL) is std::ratio. This feature, introduced in C++11, represents a compile-time rational number. It provides a way to express a ratio of two compile-time integers (numerator and denominator) and perform compile-time arithmetic with those ratios.

The std::ratio class template

The std::ratio is part of the <ratio> header and defines a class template that represents a fraction, where both the numerator and the denominator are compile-time constants of type std::intmax_tThe type `std::intmax_t` is a signed integer type capable of representing the largest signed integer on the system.. The template parameters for std::ratio are two integers: the first one is the numerator, and the second one is the denominator. By default, the denominator is 111. The ratio is always stored in reduced form, meaning the greatest common divisor (GCD) of the numerator and the denominator is 111, and the denominator is positive.

Key features and benefits

  • Compile-time function evaluation: Since std::ratio operates at compile-time, it allows for calculations resolved by the compiler, leading to faster runtime performance and reduced likelihood of runtime errors related to arithmetic operations.

  • Type safety: std::ratio provides a type-safe way to handle fractions, ensuring that operations between ratios are correctly performed and that the resulting type is a ratio.

  • Precision: Unlike floating-point arithmetic, which can introduce rounding errors, std::ratio maintains exact precision by representing ratios as fractions.

Code example

Let’s look at a code example:

#include <ratio>
#include <iostream>
int main() {
typedef std::ratio<1, 2> one_half;
typedef std::ratio<2, 3> two_thirds;
// The type of sum is std::ratio<7, 6>
typedef std::ratio_add<one_half, two_thirds> sum;
std::cout << "Sum: " << sum::num << "/" << sum::den << std::endl; // Output "Sum: 7/6"
return 0;
}
Example code of using std::ratio

This example demonstrates two ratios, one_half and two_thirds, and then adding them together using std::ratio_add. The result is a new type, sum, which represents the reduced form of the sum of these two ratios.

Advanced operations

In addition to simple arithmetic operations like addition (std::ratio_add), subtraction (std::ratio_subtract), multiplication (std::ratio_multiply), and division (std::ratio_divide), std::ratio supports comparison operations and more advanced mathematical functions. These capabilities make it a powerful tool for compile-time calculations and type transformations based on ratios. The following is a code example of an advanced operation:

#include <iostream>
#include <ratio>
int main() {
using ratio1 = std::ratio<2, 3>;
using ratio2 = std::ratio<3, 4>;
using result = std::ratio_multiply<ratio1, ratio2>;
std::cout << "Result of multiplication: " << result::num << "/" << result::den << std::endl; // Output: 1/2
return 0;
}
Example code of using std::ratio_multiply

In the example above, std::ratio_multiply is used to multiply ratio1 (2/3)(2/3) and ratio2 (3/4)(3/4). The result is 1/21/2 after multiplication and reduction.

Conclusion

std::ratio is a powerful yet underutilized feature in C++ that offers precision and type safety for fractional arithmetic at compile-time. Its utility in template metaprogramming, especially where compile-time calculations are crucial, cannot be overstated. For C++ programmers, especially those working on systems where precision and performance are critical, taking advantage of std::ratio can lead to cleaner, more efficient, and error-free code. Whether we’re dealing with time durations, physics calculations, or any other domain where ratios come into play, std::ratio is a tool worth exploring.

Ready to master C++?

Our Learn C++ course will help you build practical skills, from basic loops to complex program structures. Join now and start coding your way to success!

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved