www.digitalmars.com         C & C++   DMDScript  

D - Problem with operation of uint

reply "Naoto Ikegaya" <jacky3 mve.biglobe.ne.jp> writes:
Hello.
Please allow my awkward English.
I'm trying D.
So, I encountered a problem of uint operation.

uint x = 2147483648; //0x80000000
uint y = 0;

stdout.writeLine("2147483648 + 0 = " ~ .toString(x + y));
stdout.writeLine("size = " ~ .toString((x + y).sizeof));
stdout.writeLine("min  = " ~ .toString((x + y).min));
stdout.writeLine("max  = " ~ .toString((x + y).max));

Results..
2147483648 + 0 = -2147483648    <- WHY?
size = 4
min  = -2147483648
max  = 2147483647

I hope the result type to be uint from the oparation "uint + uint".
But above example shows that uint + uint = int!
I think that this work is maybe wrong.

My environment is Windows Me, dmd 0.82
Thanks!

--
Naoto Ikegaya <jacky3 mve.biglobe.ne.jp>
Mar 31 2004
parent J C Calvarese <jcc7 cox.net> writes:
Naoto Ikegaya wrote:

 Hello.
 Please allow my awkward English.
 I'm trying D.
 So, I encountered a problem of uint operation.
 
 uint x = 2147483648; //0x80000000
 uint y = 0;
 
 stdout.writeLine("2147483648 + 0 = " ~ .toString(x + y));
 stdout.writeLine("size = " ~ .toString((x + y).sizeof));
 stdout.writeLine("min  = " ~ .toString((x + y).min));
 stdout.writeLine("max  = " ~ .toString((x + y).max));
 
 Results..
 2147483648 + 0 = -2147483648    <- WHY?
 size = 4
 min  = -2147483648
 max  = 2147483647
 
 I hope the result type to be uint from the oparation "uint + uint".
 But above example shows that uint + uint = int!
 I think that this work is maybe wrong.
 
 My environment is Windows Me, dmd 0.82
 Thanks!
Adding an explicit cast seems to help. I don't know why your example doesn't work. It could be a compiler bug. import std.stream; void main() { uint x = 2147483648; //0x80000000 uint y = 0; stdout.writeLine("2147483648 + 0 = " ~ .toString(cast(uint) (x + y))); stdout.writeLine("size = " ~ .toString((x + y).sizeof)); stdout.writeLine("min = " ~ .toString((x + y).min)); stdout.writeLine("max = " ~ .toString((x + y).max)); } Output: 2147483648 + 0 = 2147483648 size = 4 min = -2147483648 max = 2147483647
 --
 Naoto Ikegaya <jacky3 mve.biglobe.ne.jp>
-- Justin http://jcc_7.tripod.com/d/
Mar 31 2004