int: The int data type is a 32-bit two's complement integer. Its minimum value is -2,147,483,648 and the maximum 2,147,483,647 (inclusive). Generally this type is the default choice for integer values unless there is a reason (such as those mentioned above) to choose another.
The value of total with truncating is greater than the max value of a standard Integer type. See my slightly modified script below.
According to the embarcadero docwiki System.Round Round returns a Int64 type, not a Integer type.
Also, according the embarcadero docwiki System.Trunc Trunc also returns a Int64 type.
So, if you take the Floating point value of total and display it. You will find that the value is 2,454,523,000.001647. And if you remove everything to the right of, and including, the decimal point. You will have a value of 2,454,523,000. And that value is greater than the max value of a Integer type.
And since Scripter does not seem to allow Int64 types, as shown in the Round and Trunc methods. The value you are getting would be correct for a value larger than a Integer. And the only way to fix this, is to change the returned value for Round, Trunc, and others. To return their proper Ord Types. Which in this case is Int64.
var UFDIA30, x, total;
begin
total := 0;
UFDIA30 := 24545.23
for x := 1 to 100000 do
begin
total := total + UFDIA30;
end;
ShowMessage('"UFDia30": '
+ FormatFloat('$ ####.####,##',total)+#13#13
+ '"UFDia30" rounded: '
+ FormatFloat('$ ####.####,##',Round(total)));
end;
Thank you all for the contribution. We have fixed the issue here and next release will include it. Note that the implementation of RoundProc and TruncProc in uSystemLibrary also needs to be modified:
procedure TatSystemLibrary.RoundProc(AMachine:TatVirtualMachine);
begin
with AMachine do
ReturnOutputArg( Round( GetInputArgAsFloat( 0 ) ));
end;
procedure TatSystemLibrary.TruncProc(AMachine:TatVirtualMachine);
begin
with AMachine do
ReturnOutputArg( Trunc( GetInputArgAsFloat( 0 ) ));
end;