473,320 Members | 2,024 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,320 software developers and data experts.

question about data type


#include <iostream>
using namespace std;

int main()
{
ULONG a = 1;
LONG b = -1;

cout << (a>b) << "\t" << (a==b) << "\t" << (a<b) << endl;
cout << (b<a) << "\t" << (b==a) << "\t" << (b>a) << endl;

cout << (ULONG)b << endl;

return 1;
}

The output is:

0 0 1
0 0 1
4294967295

That means when I compared LONG with ULONG (no matter which one first,
and for other unsigned data type), both numbers were converted to ULONG
and then compare. This is not what I want. One way I can do is to use
"(LONG)a > b", but is there any other better way handling this king of
problem?

For operations between int & double, both parts will be changed to
double and then proceed, as needed.

Thanks,
X
Sep 9 '05 #1
2 1293
xuatla wrote:

#include <iostream>
using namespace std;

int main()
{
ULONG a = 1;
LONG b = -1;

cout << (a>b) << "\t" << (a==b) << "\t" << (a<b) << endl;
cout << (b<a) << "\t" << (b==a) << "\t" << (b>a) << endl;

cout << (ULONG)b << endl;

return 1;
}

The output is:

0 0 1
0 0 1
4294967295

That means when I compared LONG with ULONG (no matter which one first,
and for other unsigned data type), both numbers were converted to ULONG
and then compare. This is not what I want. One way I can do is to use
"(LONG)a > b", but is there any other better way handling this king of
problem?


There is no easy way to handle this problem. It is undefined behaviour
to convert a unsigned value to a signed value when the unsigned value is
bigger than the maximum signed value for that type.

You can write code like "(LONG)a > b" and it might do one thing on your
compiler, but you have no guarantee that it will do the same thing on
another compiler.

This seems very strange (and quite wrong) to me but that's just the way
C++ is.

So there is no answer to your problem, just try and avoid it.

John
Sep 9 '05 #2
John Harrison wrote:
xuatla wrote:

#include <iostream>
using namespace std;

int main()
{
ULONG a = 1;
LONG b = -1;

cout << (a>b) << "\t" << (a==b) << "\t" << (a<b) << endl;
cout << (b<a) << "\t" << (b==a) << "\t" << (b>a) << endl;

cout << (ULONG)b << endl;

return 1;
}

The output is:

0 0 1
0 0 1
4294967295

That means when I compared LONG with ULONG (no matter which one first,
and for other unsigned data type), both numbers were converted to
ULONG and then compare. This is not what I want. One way I can do is
to use "(LONG)a > b", but is there any other better way handling this
king of problem?

There is no easy way to handle this problem. It is undefined behaviour
to convert a unsigned value to a signed value when the unsigned value is
bigger than the maximum signed value for that type.

You can write code like "(LONG)a > b" and it might do one thing on your
compiler, but you have no guarantee that it will do the same thing on
another compiler.

This seems very strange (and quite wrong) to me but that's just the way
C++ is.

So there is no answer to your problem, just try and avoid it.

John


Thank you, John :)

X
Sep 9 '05 #3

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

Similar topics

9
by: Alfonso Morra | last post by:
Hi, I am having some probs with copying memory blocks around (part of a messaging library) and I would like some confirmation to make sure that I'm going about things the right way. I have...
2
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
3
by: Henry | last post by:
I know it is possible to store dynamic propterties for applications in XML files. The app.config and the web.config files can be used to store AppSettings... I am just wondering how far one can...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
12
by: Robert.Holic | last post by:
Hi All (first time caller, long time listener), I've stumbled across a problem that I have yet to figure out, although Im sure I'll kick myself when I figure it out. Here it is: I need to...
29
by: Amar Kumar Dubedy | last post by:
implement a c++ class such that it allows us to add data members at runtime.
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.