Negate an integer



 DEVELOP > c-Plus-Plus > Negate an integer

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 06 May 2006 10:25:21 AM
Object: Negate an integer
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
.

User: "osmium"

Title: Re: Negate an integer 06 May 2006 10:35:40 AM
<wij@seed.net.tw> 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.
.
User: ""

Title: Re: Negate an integer 06 May 2006 08:09:57 PM
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
.
User: "osmium"

Title: Re: Negate an integer 06 May 2006 08:49:32 PM
<wij@seed.net.tw> 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.
.



User: "Rolf Magnus"

Title: Re: Negate an integer 06 May 2006 12:05:57 PM
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?
.
User: "Alf P. Steinbach"

Title: Re: Negate an integer 06 May 2006 09:57:12 PM
* Rolf Magnus:

wij@seed.net.tw 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?
.
User: ""

Title: Re: Negate an integer 07 May 2006 03:53:28 AM
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
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER