Contents

Chapter 2


NUMERIC CONSIDERATIONS

Granted there are some programs that don't have to do extensive mathematical calculations, but as a general rule computers have to manipulate numbers to get results. This section is devoted to things you have to consider when working with numbers.

2.1 POOR_COORDINATES package

Embedded computers often have to convert from rectangular coordinates to polar coordinates to track a target or compute an intercept trajectory. Real applications generally use three dimensional geometry, but to keep the example as simple as possible I have used only two dimensions. The POOR_COORDINATES package shown in Figure 1 is typical of how a beginning Ada programmer would write the code. It isn't a terrible package, but it could be a lot better.

There are several things right in Figure 1. Credit must be given for recognizing that transformations between coordinate systems are common to a variety of embedded computer applications, and putting them together in a library package makes them available for reuse by future programs. It was perceptive to recognize the need for data types representing rectangular and polar data points, and those type definitions certainly belong in the POOR_COORDINATES package. It is highly commendable that comments were used to tell us that the distances are in feet and the angles are in degrees. The package specification is in its own file, so it can be compiled separately. These are all good software engineering practices that are usually taught in introductory Ada classes.

2.1.1 Control of Integer Data Length

Even so, there is a fatal flaw in the POOR_COORDINATES package specification. There is likely to be trouble if this package is developed on a host computer and then recompiled for a different target. Suppose the software is developed on a VAX. Since the VAX is a 32 bit machine, VAX Ada compilers naturally use 32 bits for the predefined type integer, The distance (in feet) that can be represented by 32 bits is nearly a round trip from the earth to the moon.

The predefined integer type doesn't always have to be 32 bits. The Meridian and Alsys Ada compilers for 8086 machines use 16 bits for integer. The distance (in feet) that can be represented by 16 bits is just over 6 miles. That often isn't enough. Programs that use the POOR_COORDINATES package may work on a VAX, but not an 8086 because the number of bits in the predefined integer type is machine dependent.

The Ada Language Reference Manual allows compilers to predefine families of integer data types with names like integer, long_integer, long_long_integer, short_integer, and short_short_integer, but it doesn't say how many bits to use. A long_integer on an IBM PC has the same number of bits as an integer on a VAX.


Contents | Next ...