20Additional Library Utilities
RATIOS
The Ratio library allows you to exactly represent any finite rational number that you can use at compile time. Ratios are used in the std::chrono::duration
class discussed in the next section. Everything is defined in the <ratio>
header file and is in the std
namespace. The numerator and denominator of a rational number are represented as compile-time constants of type std::intmax_t
, which is a signed integer type with the maximum width supported by a compiler. Because of the compile-time nature of these rational numbers, using them might look a bit complicated and different than what you are used to. You cannot define a ratio
object the same way as you define normal objects, and you cannot call methods on it. Instead, you need to use type aliases. For example, the following line ...
Get Professional C++, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.