Negate an integer

W

wij

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
 
O

osmium

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.
 
R

Rolf Magnus

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?
 
W

wij

osmium said:
...
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
 
O

osmium

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.
 
A

Alf P. Steinbach

* Rolf Magnus:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top