typestar

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

  1. Int64(Frames) * 3000000 widens first, so the product does not overflow Integer.
  2. 7 div 2 gives 3 while 7 / 2 yields the Double 3.5.
  3. Round(Exposure) returns 4 and Trunc(Exposure) throws the fraction away.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Types & strings, step 4 of 27 in Language basics.

← Previous Next →