3.5.9 Fixed Point Types
A
fixed point type is either an ordinary fixed point type, or a decimal
fixed point type.
The error bound of a fixed point
type is specified as an absolute value, called the
delta of the
fixed point type.
Syntax
Name Resolution Rules
For a type defined by a
fixed_point_definition,
the
delta of the type is specified by the value of the
expression
given after the reserved word
delta; this
expression
is expected to be of any real type.
For
a type defined by a
decimal_fixed_point_definition
(a
decimal fixed point type), the number of significant decimal
digits for its first subtype (the
digits of the first subtype)
is specified by the
expression
given after the reserved word
digits; this
expression
is expected to be of any integer type.
Legality Rules
{
AI95-00100-01}
The set of values of a fixed point type comprise
the integral multiples of a number called the
small of the type.
The
machine numbers of a fixed point type are the values of the
type that can be represented exactly in every unconstrained variable
of the type.
For a type defined by an
ordinary_fixed_point_definition
(an
ordinary fixed point type), the
small may be specified
by an
attribute_definition_clause
(see
13.3); if so specified, it shall be no
greater than the
delta of the type. If not specified, the
small
of an ordinary fixed point type is an implementation-defined power of
two less than or equal to the
delta.
Implementation defined: The small
of an ordinary fixed point type.
For a decimal fixed point type, the
small
equals the
delta; the
delta shall be a power of 10. If
a
real_range_specification
is given, both bounds of the range shall be in the range –(10**
digits–1)*
delta
.. +(10**
digits–1)*
delta.
A
fixed_point_definition
is illegal if the implementation does not support a fixed point type
with the given
small and specified range or
digits.
Implementation defined: What combinations
of small, range, and digits are supported for fixed point
types.
To be honest: Or, as an obsolescent feature,
a floating point subtype is permitted — see
J.3.
Static Semantics
The base range (see
3.5)
of a fixed point type is symmetric around zero, except possibly for an
extra negative value in some implementations.
An
ordinary_fixed_point_definition
defines an ordinary fixed point type whose base range includes at least
all multiples of
small that are between the bounds specified in
the
real_range_specification.
The base range of the type does not necessarily include the specified
bounds themselves.
An
ordinary_fixed_point_definition
also defines a constrained first subtype of the type, with each bound
of its range given by the closer to zero of:
To be honest: The conversion mentioned
above is not an
implicit subtype conversion (which is something
that happens at overload resolution, see
4.6),
although it happens implicitly. Therefore, the freezing rules are not
invoked on the type (which is important so that representation items
can be given for the type).
the corresponding bound of the base range.
To be honest: The conversion mentioned
above is not an
implicit subtype conversion (which is something
that happens at overload resolution, see
4.6),
although it happens implicitly. Therefore, the freezing rules are not
invoked on the type (which is important so that representation items
can be given for the type).
Dynamic Semantics
{
AI12-0152-1}
For a
digits_constraint
on a decimal fixed point subtype with a given
delta, if it does
not have a
range_constraint,
then it specifies an implicit range –(10**
D–1)*
delta
.. +(10**
D–1)*
delta, where
D is the value
of the
simple_expression.
A
digits_constraint
is
compatible with a decimal fixed point subtype if the value
of the
simple_expression
is no greater than the
digits of the subtype, and if it specifies
(explicitly or implicitly) a range that is compatible with the subtype.
Discussion: Except for the requirement
that the
digits specified be no greater than the
digits
of the subtype being constrained, a
digits_constraint
is essentially equivalent to a
range_constraint.
Consider the following
example:
type D is delta 0.01 digits 7 range -0.00 .. 9999.99;
The compatibility rule implies that the
digits_constraint
"
digits 6" specifies an implicit range of "–9999.99
.. 9999.99". Thus, "
digits 6" is not compatible
with the constraint of D, but "
digits 6 range 0.00 .. 9999.99"
is compatible.
{
AI95-00114-01}
A value of a scalar type belongs to a constrained subtype of the type
if it belongs to the range of the subtype. Attributes like Digits and
Delta have no effect on this fundamental rule. So the obsolescent forms
of
digits_constraints
and
delta_constraints
that are called “accuracy constraints” in RM83 don't really
represent constraints on the values of the subtype, but rather primarily
affect compatibility of the “constraint” with the subtype
being “constrained”. In this sense, they might better be
called “subtype assertions” rather than “constraints”.
Note that the
digits_constraint
on a decimal fixed point subtype is a combination of an assertion about
the
digits of the subtype being further constrained, and a constraint
on the range of the subtype being defined, either explicit or implicit.
{
AI12-0152-1}
The elaboration of a
digits_constraint
consists of the elaboration of the
range_constraint,
if any.
If a
range_constraint
is given, a check is made that the bounds of the range are both in the
range –(10**
D–1)*
delta .. +(10**
D–1)*
delta,
where
D is the value of the (static)
simple_expression
given after the reserved word
digits.
If this
check fails, Constraint_Error is raised.
Implementation Requirements
The implementation shall support at least 24 bits
of precision (including the sign bit) for fixed point types.
Reason: This is sufficient to represent
Standard.Duration with a small no more than 50 milliseconds.
Implementation Permissions
Implementations are permitted to support only smalls
that are a power of two. In particular, all decimal fixed point type
declarations can be disallowed. Note however that conformance with the
Information Systems Annex requires support for decimal smalls,
and decimal fixed point type declarations with digits up to at
least 18.
Implementation Note: The accuracy requirements
for multiplication, division, and conversion (see
G.2.1,
“
Model of Floating Point Arithmetic”)
are such that support for arbitrary
smalls should be practical
without undue implementation effort. Therefore, implementations should
support fixed point types with arbitrary values for
small (within
reason). One reasonable limitation would be to limit support to fixed
point types that can be converted to the most precise floating point
type without loss of precision (so that Fixed_IO is implementable in
terms of Float_IO).
NOTE {
AI12-0442-1}
The specified bounds themselves can be outside the base range of an ordinary
fixed point type so that the range specification can be given in a natural
way, such as:
type Fraction is delta 2.0**(-15) range -1.0 .. 1.0;
{
AI12-0442-1}
With 2's complement hardware, such a type would typically have a signed
16-bit representation, using 1 bit for the sign and 15 bits for fraction,
resulting in a base range of –1.0 .. 1.0–2.0**(–15).
Examples
Examples of fixed
point types and subtypes:
type Volt is delta 0.125 range 0.0 .. 255.0;
-- A pure fraction which requires all the available
-- space in a word can be declared as the type Fraction:
type Fraction is delta System.Fine_Delta range -1.0 .. 1.0;
-- Fraction'Last = 1.0 – System.Fine_Delta
type Money is delta 0.01 digits 15; -- decimal fixed point
subtype Salary is Money digits 10;
-- Money'Last = 10.0**13 – 0.01, Salary'Last = 10.0**8 – 0.01
Inconsistencies With Ada 83
In Ada 95, S'Small always
equals S'Base'Small, so if an implementation chooses a
small for
a fixed point type smaller than required by the
delta, the value
of S'Small in Ada 95 might not be the same as it was in Ada 83.
Extensions to Ada 83
{
AI05-0005-1}
Decimal fixed point types are new, though their capabilities
are essentially similar to that available in Ada 83 with a fixed point
type whose
small equals its
delta and both are a power
of 10. However, in the Information Systems Annex, additional requirements
are placed on the support of decimal fixed point types (e.g. a minimum
of 18 digits of precision).
Wording Changes from Ada 83
The syntax rules for
fixed_point_constraint
and
fixed_accuracy_definition are removed.
The syntax rule for
fixed_point_definition
is new. A syntax rule for
delta_constraint
is included in the Obsolescent features (to be compatible with Ada 83's
fixed_point_constraint).
Wording Changes from Ada 95
{
AI95-00100-01}
Added wording to define the machine numbers of fixed point types; this
is needed by the static evaluation rules.
Incompatibilities With Ada 2012
{
AI12-0152-1}
Corrigendum: Changed the syntax so that the
value following
digits in a
digits_constraint
is a
simple_expression.
This is compatible with one very unlikely exception: if the
digits
expression is a static expression of a modular type using an unparenthesized
logical operator (like
and or
or). Parenthesizing the expression
will make it legal in that case. The change is necessary to eliminate
syntax ambguities in
derived_type_definitions.
Wording Changes from Ada 2012
{
AI12-0159-1}
Corrigendum:Added wording to define the expected type for a
digits_constraint.
This was missing since Ada 95, but as it is obvious and unchanged from
Ada 83, we don't consider it an incompatibility.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe