1389. Compile-time rational arithmetic and overflow

Section: 23.16.4 [ratio.arithmetic] Status: Resolved Submitter: BSI Opened: 2010-08-25 Last modified: 2016-02-10

Priority: Not Prioritized

View all other issues in [ratio.arithmetic].

View all issues with Resolved status.

Discussion:

Addresses GB-89

The alias representations of the ratio arithmetic templates do not allow implementations to avoid overflow, since they explicitly specify the form of the aliased template instantiation. For example ratio_multiply, ratio<2, LLONG_MAX> is required to alias ratio<2*LLONG_MAX, LLONG_MAX*2>, which overflows, so is ill-formed. However, this is trivially equal to ratio<1, 1>. It also contradicts the opening statement of 23.16.4 [ratio.arithmetic] p. 1 "implementations may use other algorithms to compute these values".

[ 2010-10-25 Daniel adds: ]

Accepting n3131 would solve this issue.

[Batavia: Resolved by accepting n3210.]

Proposed resolution:

Change the wording in 23.16.4 [ratio.arithmetic] p. 2-5 as follows:

template <class R1, class R2> using ratio_add = see below;

2 The type ratio_add<R1, R2> shall be a synonym for ratio<T1, T2> ratio<U, V> such that ratio<U, V>::num and ratio<U, V>::den are the same as the corresponding members of ratio<T1, T2> would be in the absence of arithmetic overflow where T1 has the value R1::num * R2::den + R2::num * R1::den and T2 has the value R1::den * R2::den. If the required values of ratio<U, V>::num and ratio<U, V>::den cannot be represented in intmax_t then the program is ill-formed.

template <class R1, class R2> using ratio_subtract = see below;

3 The type ratio_subtract<R1, R2> shall be a synonym for ratio<T1, T2> ratio<U, V> such that ratio<U, V>::num and ratio<U, V>::den are the same as the corresponding members of ratio<T1, T2> would be in the absence of arithmetic overflow where T1 has the value R1::num * R2::den - R2::num * R1::den and T2 has the value R1::den * R2::den. If the required values of ratio<U, V>::num and ratio<U, V>::den cannot be represented in intmax_t then the program is ill-formed.

template <class R1, class R2> using ratio_multiply = see below;

4 The type ratio_multiply<R1, R2> shall be a synonym for ratio<T1, T2> ratio<U, V> such that ratio<U, V>::num and ratio<U, V>::den are the same as the corresponding members of ratio<T1, T2> would be in the absence of arithmetic overflow where T1 has the value R1::num * R2::num and T2 has the value R1::den * R2::den. If the required values of ratio<U, V>::num and ratio<U, V>::den cannot be represented in intmax_t then the program is ill-formed.

template <class R1, class R2> using ratio_divide = see below;

5 The type ratio_divide<R1, R2> shall be a synonym for ratio<T1, T2> ratio<U, V> such that ratio<U, V>::num and ratio<U, V>::den are the same as the corresponding members of ratio<T1, T2> would be in the absence of arithmetic overflow where T1 has the value R1::num * R2::den and T2 has the value R1::den * R2::num. If the required values of ratio<U, V>::num and ratio<U, V>::den cannot be represented in intmax_t then the program is ill-formed.