Online Book Reader

Home Category

Professional C__ - Marc Gregoire [277]

By Root 1453 0
in the 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. Because of the compile time nature of these rational numbers, using them might look a bit complicated and different than usual. You cannot define a ratio object the same way as you define normal objects, and you cannot call methods on it. You need to use typedefs. For example, the following line defines a rational compile time constant representing 1/60:

typedef ratio<1, 60> r1;

The numerator and denominator of the r1 rational number are compile time constants and can be accessed as follows:

intmax_t num = r1::num;

intmax_t den = r1::den;

Remember that a ratio is a compile time constant, which means that the numerator and denominator need to be known at compile time. The following will generate a compiler error:

intmax_t n = 1;

intmax_t d = 60;

typedef ratio r1; // Error

Making n and d constants removes the compilation error:

const intmax_t n = 1;

const intmax_t d = 60;

typedef ratio r1; // Ok

Rational numbers are always normalized. For a rational number ratio, the greatest common divisor, gcd, is calculated and the numerator, num, and denominator, den, are defined as follows:

num = sign(n)*sign(d)*abs(n)/gcd

den = abs(d)/gcd

The library supports adding, subtracting, multiplying, and dividing rational numbers. Because all these operations are also happening at compile time, you cannot use the standard arithmetic operators. Instead, you need to use specific templates in combination with typedefs. The following arithmetic ratio templates are available: ratio_add, ratio_subtract, ratio_multiply, and ratio_divide. These templates calculate the result as a new ratio type. This type can be accessed with the embedded typedef called type. For example, the following code first defines two ratios, one representing 1/60 and the other representing 1/30. The ratio_add template adds those two rational numbers together to produce the result rational number, which, after normalization, will be 1/20:

typedef ratio<1, 60> r1;

typedef ratio<1, 30> r2;

typedef ratio_add::type result;

The standard also defines a number of ratio comparison templates: ratio_equal, ratio_not_equal, ratio_less, ratio_less_equal, ratio_greater, and ratio_greater_equal. Just like the arithmetic ratio templates, the ratio comparison templates are all evaluated at compile time. These comparison templates create a new type, std::integral_constant, representing the result. An integral_constant is a struct template that stores a type and a compile time constant value. For example, integral_constant stores a Boolean with value true, while integral_constant stores an integer with value 15. The result of the ratio comparison templates is either integral_constant or integral_constant. The value associated with an integral_constant can be accessed using the value data member. The following example demonstrates the use of ratio_less. Chapter 15 discusses the use of boolalpha to output true or false for Boolean values:

typedef ratio<1, 60> r1;

typedef ratio<1, 30> r2;

typedef ratio_less res;

cout << boolalpha << res::value << endl;

The following example combines everything. Note that since ratios are compile time constants, you cannot do something like cout << r1, you need to get the numerator and denominator and print them separately:

// Define a compile time rational number

typedef ratio<1, 60> r1;

cout << "1) " << r1::num << "/" << r1::den << endl;

// Get numerator and denominator

intmax_t num = r1::num;

intmax_t den = r1::den;

cout << "2) " << num << "/" << den << endl;

// Add two rational numbers

typedef ratio<1, 30> r2;

cout << "3) " << r2::num << "/" << r2::den << endl;

typedef ratio_add::type result;

cout << "4) " << result::num << "/" << result::den << endl;

// Compare two rational numbers

typedef ratio_less res;

cout << "5) " << boolalpha << res::value << endl;

Return Main Page Previous Page Next Page

®Online Book Reader