473,670 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Negate an integer

wij
Hi:

What is the protable way to negate an arithmetic integer?

template<typena me 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_li mits<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 9861
<wi*@seed.net.t w> 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<typena me 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_li mits<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<typena me 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_li mits<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_li mits<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<typena me T>
T negate(T t) {
if(t==std::nume ric_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.t w> wrote:
osmium wrote:
...
Why not do the obvious? Perhaps I don't understand what your problem is.


template<typena me T>
T negate(T t) {
if(t==std::nume ric_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<typena me 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_li mits<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_li mits<T>::min()? Or did I miss anything?


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

Probably the most protable way to do that is to check whether
-(std::numeric_l imits<T>::min() +1) == std::numeric_li mits<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_li mits<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
1914
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
1414
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. thanks
1
3773
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 question about whether or not I am interpreting a nuance of the standard correctly, and the implications of said nuance. The sections in the C99 standard (and possibly older standards) that I will reference are as follows (typed out hopefully to avoid...
7
30205
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, Laura
1
2403
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 (adr_Nazwa). I skip empty values (adr_Ewid.adr_Nazwa ! =''). I select only invoices with date after the interesting date. So now I can list all active clients but i can't negate this result set to get all inactive clients:
8
18265
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
7815
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
2687
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 Marksteiner
9
12610
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 P.S. This feature is now native in the 64 bit version, i.e.
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7415
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4209
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4390
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1792
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.