www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Integer literals

reply ric maicle <rmaicle gmail.com> writes:
I was rereading the Integer Literals section and trying out some code
when I came across a couple of error messages on integer overflows.

I have reproduced the code below for reference.

void main()
{
     // The maximum long value is ...807
     // The following line produces an error message:
     // Error: signed integer overflow
     auto a = 9_223_372_036_854_775_808;

     // The maximum ulong value is ...615
     // The following line produces an error message:
     // Error: integer overflow
     auto g = 18_446_744_073_709_551_616U;
}

I just wanted to point out that the second error message might be
consistent if it says 'Error: unsigned integer overflow' in
comparison to the first error message.

Also, I noticed under the Integer Literal section of the D reference
document (http://dlang.org/spec/lex.html) that the range of uint

   0U .. 4_294_967_296U

in the table Decimal Literal Types has a typo. Shouldn't the range
end with 4_294_967_295U?

Here's a snippet:

import std.stdio;
void main()
{
     writeln(typeof(4_294_967_295U).stringof);
     writeln(typeof(4_294_967_296U).stringof);
}
Jan 04 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 4 January 2016 at 14:54:29 UTC, ric maicle wrote:
   0U .. 4_294_967_296U

 in the table Decimal Literal Types has a typo. Shouldn't the 
 range
 end with 4_294_967_295U?
The x .. y syntax excludes y. So 0..3 covers 0, 1, 2. It excludes 3.
Jan 04 2016
parent reply ric maicle <rmaicle gmail.com> writes:
On Monday, 04 January, 2016 10:58 PM, Adam D. Ruppe wrote:
 On Monday, 4 January 2016 at 14:54:29 UTC, ric maicle wrote:
   0U .. 4_294_967_296U

 in the table Decimal Literal Types has a typo. Shouldn't the range
 end with 4_294_967_295U?
The x .. y syntax excludes y. So 0..3 covers 0, 1, 2. It excludes 3.
I copied part of the table here: 0 .. 2_147_483_647 2_147_483_648 .. 9_223_372_036_854_775_807 0L .. 9_223_372_036_854_775_807L 0U .. 4_294_967_296U 4_294_967_296U .. 18_446_744_073_709_551_615U 0UL .. 18_446_744_073_709_551_615UL If the range is x to y-1 shouldn't the rest of the other ending ranges also be one greater than the maximum value of their types?
Jan 04 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/4/16 10:33 AM, ric maicle wrote:
 On Monday, 04 January, 2016 10:58 PM, Adam D. Ruppe wrote:
 On Monday, 4 January 2016 at 14:54:29 UTC, ric maicle wrote:
   0U .. 4_294_967_296U

 in the table Decimal Literal Types has a typo. Shouldn't the range
 end with 4_294_967_295U?
The x .. y syntax excludes y. So 0..3 covers 0, 1, 2. It excludes 3.
I copied part of the table here: 0 .. 2_147_483_647 2_147_483_648 .. 9_223_372_036_854_775_807 0L .. 9_223_372_036_854_775_807L 0U .. 4_294_967_296U 4_294_967_296U .. 18_446_744_073_709_551_615U 0UL .. 18_446_744_073_709_551_615UL If the range is x to y-1 shouldn't the rest of the other ending ranges also be one greater than the maximum value of their types?
Yes, that is a typo. https://github.com/D-Programming-Language/dlang.org/pull/1181 Thanks -Steve
Jan 04 2016