liveBook · Manning

Ratio header

As explained in Learn C++ by Example

The ratio header is the C++ standard library header that declares compile-time rational arithmetic facilities such as std::ratio and ratio_multiply.

The ratio header provides compile-time rational arithmetic for representing values such as 60:1, 3:6, or 1:1,000,000,000. std::ratio reduces fractions and exposes their numerator and denominator. section 4.2.1

Its facilities support:

Reduced fractions

std::ratio<3, 6> becomes 1/2, while ratio_add can combine it with another half to produce 1/1. section 4.2.1

template<std::intmax_t Num, std::intmax_t Denom = 1> class ratio;

The declaration shows that numerator and denominator are signed integer non-type template parameters, with the denominator defaulting to 1. section 4.2.1

How Learn C++ by Example covers it

How deep it goes
Defines reduction, comparison, arithmetic, SI aliases, and custom periods, stopping short of implementation internals.
Examples
Uses reduced halves, ratio addition, nanoseconds, lossy day conversion, and a century duration.
What it assumes
Assumes basic C++ templates and chrono usage while explaining ratios through time-conversion problems.
What you can do after
Build custom chrono durations and perform compile-time rational calculations with explicit precision-loss conversions.
How it connects
Connects rational arithmetic with chrono, duration, time points, duration_cast, and SI units.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.