Boost C++ Libraries: Ticket #13101: multiplication overflow issue https://svn.boost.org/trac10/ticket/13101 <p> The following code will result in erroneous output due to integer overflow. </p> <pre class="wiki">rational x {1, INT_MAX}; rational y {1, 2}; rational z = x * y; // will result in error due to overflow </pre><p> The relevant section of the library code - rational.hpp line 500 is </p> <pre class="wiki">// 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); </pre><p> 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. </p> <p> 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. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13101 Trac 1.4.3 John Maddock Sun, 30 Jul 2017 18:25:27 GMT component changed https://svn.boost.org/trac10/ticket/13101#comment:1 https://svn.boost.org/trac10/ticket/13101#comment:1 <ul> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">rational</span> </li> </ul> Ticket