question about data type



 DEVELOP > c-Plus-Plus > question about data type

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "xuatla"
Date: 09 Sep 2005 02:29:37 PM
Object: 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
.

User: "John Harrison"

Title: Re: question about data type 09 Sep 2005 02:49:28 PM
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
.
User: "xuatla"

Title: Re: question about data type 09 Sep 2005 03:22:30 PM
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
.



  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