Thread overview
[Issue 293] New: Expression uint.max + 1 yields 0 (zero)
Aug 17, 2006
d-bugmail
Aug 17, 2006
Oskar Linde
Aug 17, 2006
BCS
Aug 17, 2006
d-bugmail
Aug 17, 2006
d-bugmail
Aug 17, 2006
d-bugmail
Aug 17, 2006
d-bugmail
August 17, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=293

           Summary: Expression uint.max + 1 yields 0 (zero)
           Product: D
           Version: 0.164
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: m.faustino@gmail.com


The expression uint.max + 1, yields 0 (zero). For example, the following code will print "4294967296 == 0":

//---------------------------------------------------------- import std.stdio;

void main()
{
        ulong u = uint.max;
        writefln(u + 1, " == ", uint.max + 1);

} //----------------------------------------------------------


-- 

August 17, 2006
d-bugmail@puremagic.com wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=293
> 
>            Summary: Expression uint.max + 1 yields 0 (zero)
>            Product: D
>            Version: 0.164
>           Platform: PC
>         OS/Version: Linux
>             Status: NEW
>           Severity: normal
>           Priority: P2
>          Component: DMD
>         AssignedTo: bugzilla@digitalmars.com
>         ReportedBy: m.faustino@gmail.com
> 
> 
> The expression uint.max + 1, yields 0 (zero). For example, the following code
> will print "4294967296 == 0":
> 
> //----------------------------------------------------------
> import std.stdio;
> 
> void main()
> {
>         ulong u = uint.max;
>         writefln(u + 1, " == ", uint.max + 1);
> 
> }
> //----------------------------------------------------------

This is not a bug. Look under Integer Promotions on http://www.digitalmars.com/d/type.html

type type of (uint) + (int) is (uint), not (ulong)

/Oskar
August 17, 2006
Oskar Linde wrote:
> d-bugmail@puremagic.com wrote:
> 
>> http://d.puremagic.com/issues/show_bug.cgi?id=293
>>
>>            Summary: Expression uint.max + 1 yields 0 (zero)
>>            Product: D
>>            Version: 0.164
>>           Platform: PC
>>         OS/Version: Linux
>>             Status: NEW
>>           Severity: normal
>>           Priority: P2
>>          Component: DMD
>>         AssignedTo: bugzilla@digitalmars.com
>>         ReportedBy: m.faustino@gmail.com
>>
>>
>> The expression uint.max + 1, yields 0 (zero). For example, the following code
>> will print "4294967296 == 0":
>>
>> //----------------------------------------------------------
>> import std.stdio;
>>
>> void main()
>> {
>>         ulong u = uint.max;
>>         writefln(u + 1, " == ", uint.max + 1);
>>
>> }
>> //----------------------------------------------------------
> 
> 
> This is not a bug. Look under Integer Promotions on http://www.digitalmars.com/d/type.html
> 
> type type of (uint) + (int) is (uint), not (ulong)
> 
> /Oskar

Shouldn't the bug be the overflow in the constant folding? I would hope that the DMD would error on overflows in constant expressions.
August 17, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=293


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #1 from bugzilla@digitalmars.com  2006-08-17 13:25 -------
That's how fixed precision integer arithmetic works when it overflows. Not a bug.


-- 

August 17, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=293





------- Comment #2 from m.faustino@gmail.com  2006-08-17 13:41 -------
(In reply to comment #1)
> That's how fixed precision integer arithmetic works when it overflows. Not a bug.
> 

So why (u + 1) doesn't overflow when (uint.max + 1) does?
In the code I wrote, aren't those two expressions semantically equivalent?


-- 

August 17, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=293





------- Comment #3 from bugzilla@digitalmars.com  2006-08-17 13:56 -------
u+1 adds 1 to a ulong, which does not overflow because it's a 64 bit type which is large enough to represent 4294967296. uint.max+1 is a uint, which does overflow because it's a 32 bit type and not large enough to hold 4294967296.

The expressions are not semantically equivalent.


-- 

August 17, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=293





------- Comment #4 from m.faustino@gmail.com  2006-08-17 14:09 -------
Ok, thanks. Sorry for the false bug report though.


--