Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Negate an integer

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      05-06-2006
<> 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.


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      05-06-2006
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?


 
Reply With Quote
 
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

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      05-07-2006
<> 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.


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-07-2006
* Rolf Magnus:
> 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?
 
Reply With Quote
 
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
negate INT_MIN jzhang918@gmail.com C Programming 2 02-10-2007 09:15 AM
How to negate a group shilpi.rustagi@beesys.com C++ 1 08-02-2006 08:03 AM
[Q] fast way to negate? Rob Mitchell Java 0 12-13-2005 02:53 AM
How do I negate a find_if condition properly? dave_if C++ 5 07-27-2005 04:47 PM
pyparsing: how to negate a grammar knguyen@megisto.com Python 3 01-09-2005 11:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57