Question

If I reach the limits of the used integer (currently on my implementation long long) I either lose the first or the last digits, depending on what is first done (multiplication or division). Looking into my implementation multiplication is done first, so I'm losing the first digits. My question is, is it defined somewhere (best in the draft) what is done first?

edit:

the implementation is like (shorted version):

count * num / den;

boost's implementation is the same.

Was it helpful?

Solution

N3797 has this to say on std::ratio:

N3797 [20.11.4] [ratio.arithmetic]

2 If it is not possible to represent U or V with intmax_t, the program is ill-formed. Otherwise, an implementation should yield correct values of U and V. If it is not possible to represent X or Y with intmax_t, the program is ill-formed unless the implementation yields correct values of U and V.

It then gives the following:

// The following cases may cause the program to be ill-formed under some implementations
static_assert(ratio_add<ratio<1,INT_MAX>, ratio<1,INT_MAX>>::num == 2,
"1/MAX+1/MAX == 2/MAX");
static_assert(ratio_add<ratio<1,INT_MAX>, ratio<1,INT_MAX>>::den == INT_MAX,
"1/MAX+1/MAX == 2/MAX");
static_assert(ratio_multiply<ratio<1,INT_MAX>, ratio<INT_MAX,2>>::num == 1,
"1/MAX * MAX/2 == 1/2");
static_assert(ratio_multiply<ratio<1,INT_MAX>, ratio<INT_MAX,2>>::den == 2,
"1/MAX * MAX/2 == 1/2");

This seems to me to suggest that if your program would overflow either the denominator or the numerator in intermediate calculations, the program is ill-formed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top