Opened 5 years ago

Last modified 5 years ago

#13101 new Feature Requests

multiplication overflow issue

Reported by: Robert Ramey Owned by: Daryle Walker
Milestone: To Be Determined Component: rational
Version: Boost 1.63.0 Severity: Problem
Keywords: Cc:

Description

The following code will result in erroneous output due to integer overflow.

rational x {1, INT_MAX};
rational y {1, 2};
rational z = x * y; // will result in error due to overflow

The relevant section of the library code - rational.hpp line 500 is

// Avoid overflow and preserve normalization
IntType gcd1 = integer::gcd(num, r_den);
IntType gcd2 = integer::gcd(r_num, den);
num = (num/gcd1) * (r_num/gcd2);
den = (den/gcd2) * (r_den/gcd1);

which, in spite of comment, does not implement any checking for overflow. The same argument applies to addition and likely other operations. Division does check for divide by zero and throws an exception.

The documentation itself is kind of cagey on the issue. In spite of having opened this issue I'm not sure what I really want to ask for. Sorry about this. Longer term I would hope to see rational and multi precision integers not include any checking and permit safe integer do it. But of course I'm not there yet.

Change History (1)

comment:1 by John Maddock, 5 years ago

Component: Nonerational
Note: See TracTickets for help on using tickets.