Velocity Reviews > C++ > Negate an integer

# Negate an integer

wij@seed.net.tw
Guest
Posts: n/a

 05-06-2006
Hi:

What is the protable way to negate an arithmetic integer?

template<typename T>
T negate(T t) {
return -t; // <-- problem
}

On my Intel machines, taking negation like the above is not
right if t is std::numeric_limits<T>::min()

So the question may be reduced to: how to detect to throw an error.

Thank you.
IJ. Wang

osmium
Guest
Posts: n/a

 05-06-2006
<(E-Mail Removed)> wrote:

> What is the protable way to negate an arithmetic integer?

I don't think a bulletproof way is possible. 0 is taken as a positive
number. Since there are an even number of numbers that can be represented
in binary, this is a problem.

> template<typename T>
> T negate(T t) {
> return -t; // <-- problem
> }
>
> On my Intel machines, taking negation like the above is not
> right if t is std::numeric_limits<T>::min()
>
> So the question may be reduced to: how to detect to throw an error.

Why not do the obvious? Perhaps I don't understand what your problem is.

Rolf Magnus
Guest
Posts: n/a

 05-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Hi:
>
> What is the protable way to negate an arithmetic integer?
>
> template<typename T>
> T negate(T t) {
> return -t; // <-- problem
> }
>
> On my Intel machines, taking negation like the above is not
> right if t is std::numeric_limits<T>::min()
>
> So the question may be reduced to: how to detect to throw an error.

Hmm, you mean how to detect that t is equal to
std::numeric_limits<T>::min()? Or did I miss anything?

wij@seed.net.tw
Guest
Posts: n/a

 05-07-2006
osmium wrote:
>...
>Why not do the obvious? Perhaps I don't understand what your problem is.

template<typename T>
T negate(T t) {
if(t==std::numeric_limits<T>::min()) {
throw error;
}
return -t;
}

1. I find no document clearly states when negate a value t yields
valid result or not.
2. The above method won't work for some machine not Intel CPU.

IJ. Wang

osmium
Guest
Posts: n/a

 05-07-2006
<(E-Mail Removed)> wrote:

> osmium wrote:
>>...
>>Why not do the obvious? Perhaps I don't understand what your problem is.

>
> template<typename T>
> T negate(T t) {
> if(t==std::numeric_limits<T>::min()) {
> throw error;
> }
> return -t;
> }
>
> 1. I find no document clearly states when negate a value t yields
> valid result or not.
> 2. The above method won't work for some machine not Intel CPU.

Are you implying that there is a compiler that lies about the value of
INT_MAX and/or INT_MIN in <climits>? I based what I said on the assumption
that they told the truth. I didn't look at your code; examining the
variables such as those two are what I had in mind. If you want an
alternative, you might be able to use one of the abs() functions somehow. I
have nothing else to offer.

Alf P. Steinbach
Guest
Posts: n/a

 05-07-2006
* Rolf Magnus:
> (E-Mail Removed) wrote:
>>
>> What is the protable way to negate an arithmetic integer?
>>
>> template<typename T>
>> T negate(T t) {
>> return -t; // <-- problem
>> }
>>
>> On my Intel machines, taking negation like the above is not
>> right if t is std::numeric_limits<T>::min()
>>
>> So the question may be reduced to: how to detect to throw an error.

>
> Hmm, you mean how to detect that t is equal to
> std::numeric_limits<T>::min()? Or did I miss anything?

He means, how to detect whether -std::numeric_limits<T>::min() exists.

Probably the most protable way to do that is to check whether
-(std::numeric_limits<T>::min()+1) == std::numeric_limits<T>::max().

Cheers.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

wij@seed.net.tw
Guest
Posts: n/a

 05-07-2006
Alf P. Steinbach wrote:
>..
>He means, how to detect whether -std::numeric_limits<T>::min() exists.
>..

Yes, that is what I meant. Thank you. (good rephrase)

IJ. Wang