In this section...
7.2.1 Predefined Numeric Types
7.2.2 Ada Model
7.2.4 Accuracy Constraints
7.2.6 Precision of Constants
7.2.7 Subexpression Evaluation
7.2.8 Relational Tests
|Summary of Guidelines from this section|
Standard. Use range and digits declarations and let the implementation do the derivation implicitly from the predefined types.
Integeron a machine with a 16-bit word. The first example below allows a compiler to choose a multiword representation if necessary.
type Second_Of_Day is range 0 .. 86_400;rather than
type Second_Of_Day is new Integer range 1 .. 86_400;or
subtype Second_Of_Day is Integer range 1 .. 86_400;
This applies to more than just numerical computation. An easy-to-overlook instance of this problem occurs if you neglect to use explicitly declared types for integer discrete ranges (array sizes, loop ranges, etc.) (see Guidelines 5.5.1 and 5.5.2). If you do not provide an explicit type when specifying index constraints and other discrete ranges, a predefined integer type is assumed.
Language Ref Manual references: 3.5.4 Integer Types, 3.5.7 Floating Point Types, 3.5.9 Fixed Point Types, 8.6 The Package Standard, C Predefined Language Environment, F Implementation-Dependent Characteristics
Language Ref Manual references: 3.5.7 Floating Point Types, 4.5.7 Accuracy of Operations with Real Operands
Floating point calculations are done with the equivalent of the implementation's predefined floating point types. The effect of extra "guard" digits in internal computations can sometimes lower the number of digits that must be specified in an Ada declaration. This may not be consistent over implementations where the program is intended to be run. It may also lead to the false conclusion that the declared types are sufficient for the accuracy required.
The numeric type declarations should be chosen to satisfy the lowest precision (smallest number of digits) that will provide the required accuracy. Careful analysis will be necessary to show that the declarations are adequate.
Language Ref Manual references: 3.5.7 Floating Point Types, 4.5.7 Accuracy of Operations with Real Operands, 13.7.3 Representation Attributes of Real Types
Language Ref Manual references: 4.5.7 Accuracy of Operations with Real Operands, F Implementation-Dependent Characteristics
Language Ref Manual references: 2.7 Comments, 3.5.4 Integer Types, 3.5.6 Real Types, 4.5.7 Accuracy of Operations with Real Operands
See also Guideline 3.2.5.
Language Ref Manual references: 2.4 Numeric Literals, 3.2 Objects and Named Numbers, 4.1 Universal Expressions, 4.9 Static Expressions and Static Subtypes
Language Ref Manual references: 3.3 Types and Subtypes, 3.3.2 Subtype Declarations, 3.4 Derived Types, 4.4 Expressions, 4.5 Operators and Expression Evaluation, 4.5.7 Accuracy of Operations with Real Operands
>=to do relational tests on real valued arguments, avoiding the
abs (X - Y) <= Float_Type'Small -- (1) abs (X - Y) <= Float_Type'Base'Small -- (2) abs (X - Y) <= abs X * Float_Type'Epsilon -- (3) abs (X - Y) <= abs X * Float_Type'Base'Epsilon -- (4)
And specifically for "equality" to zero:
abs X <= Float_Type'Small -- (1) abs X <= Float_Type'Base'Small -- (2) abs X <= abs X * Float_Type'Epsilon -- (3) abs X <= abs X * Float_Type'Base'Epsilon -- (4)
/=) are a general problem in real valued computations. Because of the way Ada comparisons are defined in terms of model intervals, it is possible for the values of the Ada comparisons
A < Band
A = Bto depend on the implementation, while
A <= Bevaluates uniformly across implementations. Note that for real values in Ada, "
A <= B" is not the same as "
not (A > B)". Further explanation can be found in Cohen (1986) pp.227-233.
Type attributes are the primary means of symbolically accessing the implementation of the Ada numeric model. When the characteristics of the model numbers are accessed symbolically, the source code is portable. The appropriate model numbers of any implementation will then be used by the generated code.
Although zero is technically not a special case, it is often overlooked because it looks like the simplest and, therefore, safest case. But in reality, each time comparisons involve small values, evaluate the situation to determine which technique is appropriate.
Regardless of language, real valued computations have inaccuracy. That the corresponding mathematical operations have algebraic properties usually introduces some confusion. This guideline explains how Ada deals with the problem that most languages face.
Language Ref Manual references: 4.5.2 Relational Operators and Membership Tests