Numeric types in Delphi
An observatory counts frames, sums bytes and measures an exposure in seconds.
program NumbersTypes;
uses SysUtils;
var
Frames: Integer;
TotalBytes: Int64;
Exposure: Double;
LongEnough: Boolean;
begin
Frames := 1440;
TotalBytes := Int64(Frames) * 3000000; // widen before multiplying
Exposure := 7 / 2; // / always yields a Double
LongEnough := Exposure > 3;
WriteLn(Format('frames %d, bytes %d', [Frames, TotalBytes]));
WriteLn(Format('7 div 2 = %d, 7 / 2 = %.1f', [7 div 2, Exposure]));
// Round picks the nearest whole number, Trunc drops the fraction.
WriteLn(Format('round %d, trunc %d', [Round(Exposure), Trunc(Exposure)]));
WriteLn('long enough: ', LongEnough);
end.
How it works
Int64(Frames) * 3000000widens first, so the product does not overflowInteger.7 div 2gives 3 while7 / 2yields theDouble3.5.Round(Exposure)returns 4 andTrunc(Exposure)throws the fraction away.
Keywords and builtins used here
BooleanDoubleFormatInt64IntegerRoundTruncWriteLnbegindivendprogramusesvar
The run, in numbers
- Lines
- 19
- Characters to type
- 617
- Tokens
- 116
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 93 seconds.
Step 1 of 3 in Types & strings, step 4 of 27 in Language basics.