Title
Compile-time rational arithmetic and overflow
Status
resolved
Section
[ratio.arithmetic]
Submitter
BSI

Created on 2010-08-25.00:00:00 last changed 170 months ago

Messages

Date: 2011-05-09.23:20:35

Proposed resolution:

Change the wording in [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.

Date: 2011-04-25.21:37:53

[ Batavia: Resolved by accepting n3210. ]

Date: 2010-10-25.00:00:00

[ 2010-10-25 Daniel adds: ]

Accepting n3131 would solve this issue.

Date: 2010-10-31.14:51:51

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 [ratio.arithmetic] p. 1 "implementations may use other algorithms to compute these values".

History
Date User Action Args
2010-11-18 12:46:23adminsetstatus: nad editorial -> resolved
2010-11-13 01:42:55adminsetmessages: + msg5346
2010-11-13 01:42:55adminsetstatus: open -> nad editorial
2010-10-25 21:29:27adminsetmessages: + msg5133
2010-10-24 10:50:39adminsetmessages: + msg5007
2010-08-25 00:00:00admincreate