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.
Note:
See TracTickets
for help on using tickets.