Figure 7.
Dimensional division.
------------------------------------------------------------
-- The Dimensionless function can be used in special
-- operators which convert unit automatically.
with DIM_INT_32, STANDARD_INTEGERS;
procedure Program_Fragment is
-- create some dimensional data types
type Feet is new DIM_INT_32.Units;
type Feet_per_sec is new DIM_INT_32.Units;
type Milliseconds is new DIM_INT_32.Units;
-- define some dimensioned objects
MOVEMENT, PRESENT_POSITION, PAST_POSITION : Feet;
SPEED : Feet_per_sec;
DELTA_T : Milliseconds;
-- Tell Ada how to divide Feet by Milliseconds
-- to get an answer in Feet_per_sec (including
-- the scale factor of 1000.)
function "/"(LEFT : Feet; RIGHT : Milliseconds)
return Feet_per_sec is
use STANDARD_INTEGERS;
X, Y, Z : Integer_32;
begin
X := Dimensionless(LEFT);
Y := Dimensionless(RIGHT);
Z := Integer_32(1000.0 * float(X) / float(Y));
return Type_Convert(Z);
end "/";
begin
loop
-- Missing statements here have assigned values to
-- PRESENT_POSITION, PAST_POSITION, and DELTA_T.
-- Ada checks the next two lines for
-- dimensional consistency, and they are OK.
MOVEMENT := PRESENT_POSITION - PAST_POSITION;
SPEED := MOVEMENT / DELTA_T;
-- Do something with SPEED and PRESENT_POSITION
-- and exit the loop if appropriate.
PAST_POSITION := PRESENT_POSITION;
end loop;
end Program_Fragment;