[net.lang.ada] Fixed point data in Ada

bcw (11/02/82)

From:	Bruce C. Wright @ Duke University
Re:	Fixed point data types in Ada

The term "poor man's floating point" to describe the Ada fixed-point data
type is not meant to be an exact description of the Ada data type (obviously
a fixed radix point does not "float"), but was meant as a way of trying to
describe the data type to people who had likely not encountered such a type
in PL/I or Cobol.

The term is not mine;  consider this phrase from the "Rationale for the
Design of the ADA Programming Language" by Ichbiah, Heliard, Roubine, Barnes,
Krieg-Brueckner, and Wichmann:  Section 5.1.2, paragraph 5 refers to the
need for a "cheap floating point";  my precise term was borrowed from Peter
Wegener in his book "Programming with Ada: an Introduction by means of
Graduated Examples," Section 3.3, page 96.

Secondly, although it is true that a number of real-world objects are defined
in terms of fixed-point numbers (like time, money, etc), the Ada fixed-point
facility is **not** sufficient for manipulating such things with 100%
accuracy, but is subject to error.  A private communication from Jerry
Schwartz (harpo!eagle!jerry) indicates that the final report did clear up
the ambiguity:  the delta value must be a power of two and must be represented
exactly.  Note that this specifically excludes money or time data from use
with the Ada fixed point data type, unless you don't mind rounding error.
Also, fixed point is now apparently "optional" for the implementation (what
became of the requirement for no subsets? Sounds like a subset to me!).

			Bruce C. Wright @ Duke University

donald (11/02/82)

What's all the fuss about the fixed-point computation?  Fixed point
numbers are just values which have an absolute error rather than
a relative error as in floating point.  The way that fixed point numbers
in Ada are obviously intended to be implemented is as scaled binary
integers (note the power of two delta), to satisfy the urges of those
that want to perform "real" arithmetic on little microprocessors controlling
cruise missiles that don't have hardware floating-point (and can't take
software floating-point).  Of course, fixed-point computations are
much nastier with regard to error analyses, but as long as that cruise
missile reaches them ruskies...

jerry (11/03/82)

This subject is more complicated than meets the eye.

I will present some statements from the LRM

LRM 3.5.9-3 defines the "delta" of an Ada 
fixed point type as the value given in the declaration 
which may be any positive real value.

LRM 3.5.9-4 says that the "model numbers" for
a fixed point type are multiples of "small"

LRM 3.5.9-5 says "the number small is
choosen as the largest power of two that is not greater
than the specified delta.  Alternatively, it is
possible to specify the value of small by a length clause.
(see 13.2)"

LRM 13.2 defines "length clauses" which are a particular
kind of "representation clause".  In particular  LRM 13.2-9
Defines a clause of the form

	for T'small use simple_expression

and says "the type T must be a fixed point type.  The expression
must be a static expression of some real type; its value
must not be greater than the delta of the type.  The effect
of the length clause is to use this value of small for the
representation of values of the fixed point type."

Taking all the above together it would seem that dollars 
could be specified by

	type money is delta .01 ;
	for money'small use .01 ;

This should get a representation in which values of type
money are represented in "cents".  

Great!  But before you get excited let me quote one more
paragraph of the LRM 3.5.9-11: "The fixed point declaration
is illegal if no implementation defined type satisfies
these requirements". (There must be at least
one anonymous fixed point type according to LRM 3.5.9-7)

My interpretation of this is: if your Ada compiler accepts the 
declaration and length clause it will give you the 
representation you want.  But there is no guarantee
that it will accept it.

I hope this clears up some of the confusion.

Jerry Schwarz
harpo!eagle!jerry