TYPE ATTRIBUTES AND TYPE CONVERSIONS


1. ADA ATTRIBUTES

Ada allows the programmer to access constants that give information about the default properties of a given type. Such constants are referred to as attributes. Some common Ada attributes include:

TypeAttributes
integerfirst, last
floatdigits, small, large

Example usage:

INTEGER'FIRST
INTEGER'LAST
FLOAT'DIGITS
FLOAT'SMALL
FLOAT'LARGE

The attributes first and last specify the default Ada integer range, while the attributes small and large specify the default Ada floating point range. The attribute digits then specifies the default precision for Ada floating point numbers.


1.1. Further notes

  1. Two's complement number notation.

  2. Fixed and floating point storage.


2. EXAMPLE PROBLEM ADA ATTRIBUTES


2.1. Requirements

To develop, using the Ada attributes facility, a test program to determine the range of the integer type and the range and precision of the floating point type available on a particular computer.


2.2. Design

Using a top down analysis (see below) approach we can identify, one top level operation, two level 2 operations and five level 3 operations.

TOP DOWN DESIGN

The level 3 operations are trivial enough not to warrant procedures of their own. Thus we will only design 3 procedures, one top level and two second level, as follow:

  1. ATTRIBUTES (top level procedure) - Calls to INTEGER_ATTRIBUTES and FLOAT_ATTRIBUTES.
  2. INTEGER_ATTRIBUTES (level 2 procedure) - Output first and last integer.
  3. FLOAT_ATTRIBUTES (level 2 procedure) - Output first and last float and number of digits.

Note that there is no input and that none of the procedures require any parameters. Nassi-Shneiderman designs for these three procedures are given below.

NASSI_SHNEIDERMAN CHART

2.3. Implementation

Implementation commences with the level 1 procedure using stubs for level 2. Thus:

-- ADA ATTRIBUTES
-- 6 August 1997
-- Frans Coenen
-- Dept Computer Science, University of Liverpool

with TEXT_IO;
use TEXT_IO;

procedure ATTRIBUTES is

------------------------------------------------------------
-- INTEGER TYPE ATTRIBUTES PROCEDURE
        procedure INTEGER_ATTRIBUTES is
        begin
                PUT("Integer attributes procedure");
                NEW_LINE;
        end INTEGER_ATTRIBUTES;

-----------------------------------------------------------
-- FLOATING POINT TYPE ATTRIBUTES PROCEDURE
        procedure FLOAT_ATTRIBUTES is
        begin
                PUT("Float attributes procedure");
                NEW_LINE;
        end FLOAT_ATTRIBUTES;

-----------------------------------------------------------
-- TOP LEVEL PROCEDURE
begin
        INTEGER_ATTRIBUTES;
        FLOAT_ATTRIBUTES;
end ATTRIBUTES;

On completion of this level a test run should be carried out to ensure that the program is working correctly. The expected output would be as follows:

Integer attributes procedure
Float attributes procedure

Assuming a successful test, the next stage is to implement the level 2 procedures:

-- ADA ATTRIBUTES
-- 6 August 1997
-- Frans Coenen
-- Dept Computer Science, University of Liverpool

with TEXT_IO;
use TEXT_IO;

procedure ATTRIBUTES is
        package INTEGER_INOUT is new INTEGER_IO(INTEGER);
        use INTEGER_INOUT;

------------------------------------------------------------
-- INTEGER TYPE ATTRIBUTES PROCEDURE
        procedure INTEGER_ATTRIBUTES is
        begin
                PUT("first  = ");
                PUT(INTEGER'FIRST);
                NEW_LINE;
                PUT("last   = ");
                PUT(INTEGER'LAST);
                NEW_LINE;
        end INTEGER_ATTRIBUTES;

-----------------------------------------------------------
-- FLOATING POINT TYPE ATTRIBUTES PROCEDURE
        procedure FLOAT_ATTRIBUTES is
                package FLOAT_INOUT is new FLOAT_IO(FLOAT);
                use FLOAT_INOUT;

        begin
                PUT("digits = ");
                PUT(FLOAT'DIGITS);
                NEW_LINE;
                PUT("small  = ");
                PUT(FLOAT'SMALL);
                NEW_LINE;
                PUT("large  = ");
                PUT(FLOAT'LARGE);
                NEW_LINE;
        end FLOAT_ATTRIBUTES;

-----------------------------------------------------------
-- TOP LEVEL PROCEDURE
begin
        INTEGER_ATTRIBUTES;
        FLOAT_ATTRIBUTES;
end ATTRIBUTES;

Note that we have used the INTEGER_IO and FLOAT_IO templates contained in TEXT_IO to create appropriate I/O packages. We have placed the FLOAT_INOUT package creation statement with the FLOAT_ATTRIBUTES procedure declarations because it only needs to be visible from within this procedure while the INTEGER_INOUT package must be globally visible, i.e. visible from both the INTEGER_ATTRIBUTES and FLOAT_ATTRIBUTES procedures.


2.4. Testing

The above code includes no input therefore a single execution will suffice to test the implementation. The output can be expected to be of the form:

first  = -2147483648
last   =  2147483647
digits =          15
small  =  1.94469227433161E-62
large  =  2.57110087081438E+61

although the precise nature of this output will be machine dependent. Note that the integer result indicates a 32 bit (2^32 = 2147483648) two's complement number notation. Remember that figures such as 1.94469227433161E-62 should be read as 1.94469227433161x10^-62.


Example Problem Ada Attributes Report.


3. TYPE CONVERSION

It is sometimes necessary to convert a data item of one type to another type. For example when it is necessary to perform some arithmetic using data items of different types. Ada type conversions have the following form:

T(N)

where T is the name of a numeric type and N has another numeric type. The result is of type T. Example:

with CS_IO;
use CS_IO;

procedure CONVERSION is
        INT_VALUE: INTEGER := 1;
        FLOAT_VALUE: FLOAT := 2.0;
begin
        PUT(FLOAT(INT_VALUE) + FLOAT_VALUE);
        NEW_LINE;
end CONVERSION;

Here we have converted an integer type to a floating point type.




Created and maintained by Frans Coenen. Last updated 11 October 1999