Re: integer abs() overflow
On Jul 5, 10:37 pm, Pete Becker <p...@versatilecoding.com> wrote:
Jonathan Lee wrote:
Use std::abs.
It exhibits the same problem (see below).
No, the code that uses it exhibits the same problem. <g>
It seems that std::abs returns a signed value, not unsigned.
Yes, that's what it's required to do.
Note however that his original code didn't. It returned the
corresponding unsigned type.
// Example ----------------------------------------
#include <cstdlib>
#include <iostream>
#include <limits>
using ::std::cout;
using ::std::endl;
int main() {
int x = std::numeric_limits<int>::min();
cout << std::abs(x) << endl;
cout << std::abs(x + 1) << endl;
}
// Output ----------------------------------------
-2147483648
2147483647
If the returned value is negative then the result wasn't
representable as a non-negative value. The point, though, is
that this is managed for you by the standard library, so you
don't have to deal with the details of your particular
implementation's representation.
The standard library function has undefined behavior if passed a
value for which the absolute value is not representable. That's
not necessarily "dealing with the details of your particular
implementation's representation" in an acceptable fashion.
Given that the standard does defined conversion of signed to
unsigned, I think something like the following would work:
return ( input >= - std::numeric_limits< long >::max()
&& input < 0 )
? static_cast< unsigned long >( -input )
: static_cast< unsigned long >( input ) ;
Of course, in practice, if the machine doesn't use 2's
complement, or uses 2's complement without any checking, then
just casting the results of the standard abs() to the unsigned
type will do the trick. And off hand, I've never heard of an
implementation which did any checking here.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34