Professional C__ - Marc Gregoire [278]
Code snippet from Ratio\ratios.cpp
The output should be as follows:
1) 1/60
2) 1/60
3) 1/30
4) 1/20
5) false
The library provides a number of SI (Système International) typedefs for your convenience. They are as follows:
typedef ratio<1, 1000000000000000000000000> yocto; // *
typedef ratio<1, 1000000000000000000000> zepto; // *
typedef ratio<1, 1000000000000000000> atto;
typedef ratio<1, 1000000000000000> femto;
typedef ratio<1, 1000000000000> pico;
typedef ratio<1, 1000000000> nano;
typedef ratio<1, 1000000> micro;
typedef ratio<1, 1000> milli;
typedef ratio<1, 100> centi;
typedef ratio<1, 10> deci;
typedef ratio< 10, 1> deca;
typedef ratio< 100, 1> hecto;
typedef ratio< 1000, 1> kilo;
typedef ratio< 1000000, 1> mega;
typedef ratio< 1000000000, 1> giga;
typedef ratio< 1000000000000, 1> tera;
typedef ratio< 1000000000000000, 1> peta;
typedef ratio< 1000000000000000000, 1> exa;
typedef ratio< 1000000000000000000000, 1> zetta; // *
typedef ratio<1000000000000000000000000, 1> yotta; // *
The SI units with an asterisk at the end are defined only if your compiler can represent the constant numerator and denominator values for those typedefs as an intmax_t. An example on how to use these predefined SI units is given during the discussion of durations later in this chapter.
THE CHRONO LIBRARY
The C++11 Chrono library is a collection of classes to work with times. The library consists of the following components:
Duration
Clock
Time point
Everything is defined in the std::chrono namespace and requires you to include the Duration A duration, which represents an interval between two points in time, is specified by the templatized duration class. The duration class stores a number of ticks and a tick period. The tick period is the time in seconds between two ticks and is represented as a compile time ratio constant, which means it could be a fraction of a second. Ratios are discussed earlier in this chapter. The duration template accepts two template parameters and is defined as follows: template The first template parameter, Rep, is the type of variable storing the number of ticks and should be an arithmetic type, for example long, double, and so on. The second template parameter, Period, is the rational constant representing the period of a tick. If you don’t specify the tick period, the default value ratio<1> will be used, which represents a tick period of 1 second. Three constructors are provided: the default constructor; one that accepts a single value, the number of ticks; and one that accepts another duration. The latter can be used to convert from one duration to another duration, for example from minutes to seconds. An example is given later in this section. Durations support arithmetic operations such as +, -, *, /, %, ++, --, +=, -=, *=, /= and %=, and support the comparison operators. The class contains the following methods: METHOD DESCRIPTION Rep count() const Returns the duration value as the number of ticks. The return type is the type specified as parameter to the duration template. static duration zero() Returns a duration with a duration value equivalent to 0. static duration min() static duration max() Returns a duration with the minimum/maximum possible duration value representable by the type specified as parameter to the duration template. Let’s see how durations can be used in actual code. A duration where each tick is one second can be defined as follows: duration Because ratio<1> is the default tick period, this is the same as writing the following: duration The following specifies a duration in minutes (tick period = 60 seconds): duration To define a duration where each tick period is a sixtieth of a second, use the following: duration As seen earlier in this chapter, the