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:
Type | Attributes |
---|---|
integer | first, last |
float | digits, 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.
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.
Using a top down analysis (see below) approach we can identify, one top level operation, two level 2 operations and five level 3 operations.
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:
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.
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.
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.
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