Opened 17 years ago

Closed 16 years ago

#565 closed Patches (Fixed)

rational::operator< fails for unsigned value types

Reported by: dbenbenn Owned by: Jonathan Turkanis
Milestone: Component: None
Version: None Severity:
Keywords: Cc:

Description

There's a minor bug in rational.hpp when using unsigned
values.  Consider the following simple program:

int main(void) {
  boost::rational<unsigned int> x = 0;
  assert(x.operator<(1));
}

The assertion fails.  The reason is that in
rational.hpp::rational<IntType>::operator< (param_type
i) are the lines

   if (num > zero)
       return (num/den) < i;
   else
       return -i < (-num/den);
The last line is bogus for unsigned types if num == 0
and i > 0.  The problem can be fixed by changing (num >
zero) to (num >= zero).

Another issue with the same section of code is that it
uses up to 5 comparisons of IntType values.  Attached
is a patch that fixes the bug described, and also makes
the function only perform up to 3 comparisons.  That
can make a difference, of course, if IntType is a
complicated type with a slow comparison operator.

Change History (1)

comment:1 by Daryle Walker, 16 years ago

Status: assignedclosed
Logged In: YES 
user_id=551024

An extra change while fixing bug #798357 fixes
this issue, without using this patch. The current
solution performs a floor-rounding division and
modulus, followed by a comparison. The floor
division involves either one or two extra comparisons,
for possibly converting from a truncating division.
(The assert currently there uses another comparison.)

Note: See TracTickets for help on using tickets.