Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug or limit of Delphi's max. constant integer values? [duplicate]

const
  minDriveFreeSpace: Int64 = 1024*1024*1024*99;

and

var
  minDriveFreeSpace: Int64;
begin
  minDriveFreeSpace := 1024*1024*1024*99;

Will emit:

[dcc32 Error] DataStoreLocator.pas(92): E2099 Overflow in conversion or arithmetic operation

Is this a bug or limit of Delphi's max. constant integer values?

like image 514
Edwin Yip Avatar asked May 13 '14 15:05

Edwin Yip


1 Answers

You need to cast at least one value on the right side to Int64. For instance, both of these compile perfectly well on XE6:

const
  minDriveFreeSpace = Int64(1024) * 1024 * 1024 * 99;

var
  minDriveFreeSpace2: Int64;
begin
  minDriveFreeSpace2 := Int64(1024)*1024*1024*99;

Note that it can be any one of the rvalues that are cast. For instance, this works equally as well:

const
  minDriveFreeSpace = 1024 * 1024 * 1024 * Int64(99);

This is documented in the Delphi language guide (although rather poorly) - emphasis mine:

In general, arithmetic operations on integers return a value of type Integer, which is equivalent to the 32-bit LongInt. Operations return a value of type Int64 only when performed on one or more Int64 operands. Therefore, the following code produces incorrect results:

var
I: Integer;
J: Int64;
... 
I := High(Integer);
J := I + 1;

To get an Int64 return value in this situation, cast I as Int64:

...
J := Int64(I) + 1;
like image 76
Ken White Avatar answered Nov 08 '22 02:11

Ken White