473,387 Members | 1,321 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Negate an integer

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

May 6 '06 #1
6 9842
<wi*@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.
May 6 '06 #2
wi*@seed.net.tw 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?
May 6 '06 #3
wij
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

May 7 '06 #4
<wi*@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.
May 7 '06 #5
* Rolf Magnus:
wi*@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?
May 7 '06 #6
wij
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

May 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lucas Tam | last post by:
How do I do the following? Select Case(SomeValue) Case NOT A, M, Q, Y 'Do stuff if it is not A,M,Q,Y End Select
1
by: shilpi.rustagi | last post by:
hi again.. i have a basic regular expression problem. we can negate a single charecter like this . but i want to negate a whole group e.g (<\tag>) hw can i do that can anyone help on it. ...
1
by: charles_gero | last post by:
Hi all, I had a question about the topics in the subject and posted to comp.std.c, but feel it may also be appropriate here. Please excuse this crosspost if it is in bad form. I have a...
7
by: laura | last post by:
Hi, I have a variable of type double. I need to know if there is an integer number store there. How can I test that ? I also have a default precision for doing this operation. Many thanks,...
1
by: Mike | last post by:
I' d like to list all inactive clients. Inactive client is a client who hasn't had invoice for 2 months. I use INNER JOIN to join invoice view (vwDok4FSFZGrid) and clients addresses table...
8
by: Summercool | last post by:
somebody who is a regular expression guru... how do you negate a word and grep for all words that is tire but not snow tire or
3
by: =?ISO-8859-1?Q?David_Tr=E9mouilles?= | last post by:
Hi, Is there any better (shorter) way to negate a boolean list than: ? I tried: but it seems that "not" is not a function. Thanks in advance,
2
by: Franz Marksteiner | last post by:
Hi folks, is there a way to negate a glob() pattern? What I want is to get a tree structure, e.g. all .php files *and* all directories. How would I start here? -- Freundliche Grüße, Franz...
9
by: jacob navia | last post by:
Hi I am incorporating 128 Bit integer code into lcc-win and it would be nice to have some code to test this feature. Has anyone here code that uses 128 bit integers? Thanks in advance ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.