[Home] [Prev] [Next] [Index]

4.5 Operators and Expression Evaluation

4.5 Operators and Expression Evaluation

1
[The language defines the following six categories of operators (given in order of increasing precedence).  The corresponding operator_symbols, and only those, can be used as designators in declarations of functions for user-defined operators.  See 6.6, "Overloading of Operators".]

Syntax

2
logical_operator ::=  and | or  | xor

3
relational_operator ::=  =   | /=  | <   | <= | > | >=

4
binary_adding_operator ::=  +   | -   | &

5
unary_adding_operator ::=  +   | -

6
multiplying_operator ::=  *   | /   | mod | rem

7
highest_precedence_operator ::=  **  | abs | not

7.a
Discussion:  Some of the above syntactic categories are not used in other syntax rules.  They are just used for classification. The others are used for both classification and parsing.

Static Semantics

8
For a sequence of operators of the same precedence level, the operators are associated with their operands in textual order from left to right. Parentheses can be used to impose specific associations.

8.a
Discussion:  The left-associativity is not directly inherent in the grammar of , though in 1.1.4 the definition of the metasymbols {} implies left associativity.  So this could be seen as redundant, depending on how literally one interprets the definition of the {} metasymbols.

8.b
See the Implementation Permissions below regarding flexibility in reassociating operators of the same precedence.

9
For each form of type definition, certain of the above operators are predefined; that is, they are implicitly declared immediately after the type definition. For each such implicit operator declaration, the parameters are called Left and Right for binary operators; the single parameter is called Right for unary operators. [An expression of the form X op Y, where op is a binary operator, is equivalent to a function_call of the form "op"(X, Y). An expression of the form op Y, where op is a unary operator, is equivalent to a function_call of the form "op"(Y). The predefined operators and their effects are described in subclauses 4.5.1 through 4.5.6.]

Dynamic Semantics

10
[The predefined operations on integer types either yield the mathematically correct result or raise the exception Constraint_Error. For implementations that support the Numerics Annex, the predefined operations on real types yield results whose accuracy is defined in Annex G, or raise the exception Constraint_Error.]

10.a
To be honest: Predefined operations on real types can "silently" give wrong results when the Machine_Overflows attribute is false, and the computation overflows.

Implementation Requirements

11
The implementation of a predefined operator that delivers a result of an integer or fixed point type may raise Constraint_Error only if the result is outside the base range of the result type.

12
The implementation of a predefined operator that delivers a result of a floating point type may raise Constraint_Error only if the result is outside the safe range of the result type.

12.a
To be honest: An exception is made for exponentiation by a negative exponent in 4.5.6.

Implementation Permissions

13
For a sequence of predefined operators of the same precedence level (and in the absence of parentheses imposing a specific association), an implementation may impose any association of the operators with operands so long as the result produced is an allowed result for the left-to-right association, but ignoring the potential for failure of language-defined checks in either the left-to-right or chosen order of association.

13.a
Discussion:  Note that the permission to reassociate the operands in any way subject to producing a result allowed for the left-to-right association is not much help for most floating point operators, since reassociation may introduce significantly different round-off errors, delivering a result that is outside the model interval for the left-to-right association.  Similar problems arise for division with integer or fixed point operands.

13.b
Note that this permission does not apply to user-defined operators.

NOTES

14 11
The two operands of an expression of the form X op Y, where op is a binary operator, are evaluated in an arbitrary order, as for any function_call (see 6.4).

Examples

15
Examples of precedence:

16
not Sunny or Warm    --  same as (not Sunny) or Warm
X > 4.0 and Y > 0.0  --  same as (X > 4.0) and (Y > 0.0)

17
-4.0*A**2            --  same as -(4.0 * (A**2))
abs(1 + A) + B       --  same as (abs (1 + A)) + B
Y**(-3)              --  parentheses are necessary
A / B * C            --  same as (A/B)*C
A + (B + C)          --  evaluate B + C before adding it to A

Wording Changes From Ada 83

17.a
We don't give a detailed definition of precedence, since it is all implicit in the syntax rules anyway.

17.b
The permission to reassociate is moved here from RM83-11.6(5), so it is closer to the rules defining operator association.

4.5.1 Logical Operators and Short-circuit Control Forms

Name Resolution Rules

1
An expression consisting of two relations connected by and then or or else (a short-circuit control form) shall resolve to be of some boolean type; the expected type for both relations is that same boolean type.

1.a
Reason: This rule is written this way so that overload resolution treats the two operands symmetrically; the resolution of overloading present in either one can benefit from the resolution of the other. Furthermore, the type expected by context can help.

Static Semantics

2
The following logical operators are predefined for every boolean type T, for every modular type T, and for every one-dimensional array type T whose component type is a boolean type:

3
function "and"(Left, Right : T) return T
function "or" (Left, Right : T) return T
function "xor"(Left, Right : T) return T

3.a
To be honest: For predefined operators, the parameter and result subtypes shown as T are actually the unconstrained subtype of the type.

4
For boolean types, the predefined logical operators and, or, and xor perform the conventional operations of conjunction, inclusive disjunction, and exclusive disjunction, respectively.

5
For modular types, the predefined logical operators are defined on a bit-by-bit basis, using the binary representation of the value of the operands to yield a binary representation for the result, where zero represents False and one represents True. If this result is outside the base range of the type, a final subtraction by the modulus is performed to bring the result into the base range of the type.

6
The logical operators on arrays are performed on a component-by-component basis on matching components (as for equality -see 4.5.2), using the predefined logical operator for the component type.  The bounds of the resulting array are those of the left operand.

Dynamic Semantics

7
The short-circuit control forms and then and or else deliver the same result as the corresponding predefined and and or operators for boolean types, except that the left operand is always evaluated first, and the right operand is not evaluated if the value of the left operand determines the result.

8
For the logical operators on arrays, a check is made that for each component of the left operand there is a matching component of the right operand, and vice versa. Also, a check is made that each component of the result belongs to the component subtype. The exception Constraint_Error is raised if either of the above checks fails.

8.a
Discussion:  The check against the component subtype is per AI-00535.

NOTES

9 12
The conventional meaning of the logical operators is given by the following truth table:

10 AB(A and B)(A or B)(A xor B)

      


TrueTrueTrueTrueFalse         

TrueFalseFalseTrueTrue         

FalseTrueFalseTrueTrue         

FalseFalseFalseFalseFalse

Examples

11
Examples of logical operators:

12
Sunny or Warm
Filter(1 .. 10) and Filter(15 .. 24)   --   see 3.6.1

13
Examples of short-circuit control forms:

14
Next_Car.Owner /= null and then Next_Car.Owner.Age > 25   --   see 3.10.1
N = 0 or else A(N) = Hit_Value

4.5.2 Relational Operators and Membership Tests

1
[The equality operators = (equals) and /= (not equals) are predefined for nonlimited types. The other relational_operators are the ordering operators < (less than), <= (less than or equal), > (greater than), and >= (greater than or equal). The ordering operators are predefined for scalar types, and for discrete array types, that is, one-dimensional array types whose components are of a discrete type.

1.a
Ramification: The equality operators are not defined for every nonlimited type - see below for the exact rule.

2
A membership test, using in or not in, determines whether or not a value belongs to a given subtype or range, or has a tag that identifies a type that is covered by a given type. Membership tests are allowed for all types.]

Name Resolution Rules

3
The tested type of a membership test is the type of the range or the type determined by the subtype_mark. If the tested type is tagged, then the simple_expression shall resolve to be of a type that covers or is covered by the tested type; if untagged, the expected type for the simple_expression is the tested type.

3.a
Reason: The part of the rule for untagged types is stated in a way that ensures that operands like null are still legal as operands of a membership test.

3.b
The significance of "covers or is covered by" is that we allow the simple_expression to be of any class-wide type that covers the tested type, not just the one rooted at the tested type.

Legality Rules

4
For a membership test, if the simple_expression is of a tagged class-wide type, then the tested type shall be (visibly) tagged.

4.a
Ramification: Untagged types covered by the tagged class-wide type are not permitted.  Such types can exist if they are descendants of a private type whose full type is tagged. This rule is intended to avoid confusion since such derivatives don't have their "own" tag, and hence are indistinguishable from one another at run time once converted to a covering class-wide type.

Static Semantics

5
The result type of a membership test is the predefined type Boolean.

6
The equality operators are predefined for every specific type T that is not limited, and not an anonymous access type, with the following specifications:

7
function "=" (Left, Right : T) return Boolean
function "/="(Left, Right : T) return Boolean

8
The ordering operators are predefined for every specific scalar type T, and for every discrete array type T, with the following specifications:

9
function "<" (Left, Right : T) return Boolean
function "<="(Left, Right : T) return Boolean
function ">" (Left, Right : T) return Boolean
function ">="(Left, Right : T) return Boolean

Dynamic Semantics

10
For discrete types, the predefined relational operators are defined in terms of corresponding mathematical operations on the position numbers of the values of the operands.

11
For real types, the predefined relational operators are defined in terms of the corresponding mathematical operations on the values of the operands, subject to the accuracy of the type.

11.a
Ramification: For floating point types, the results of comparing nearly equal values depends on the accuracy of the implementation (see G.2.1, "Model of Floating Point Arithmetic" for implementations that support the Numerics Annex).

11.b
Implementation Note: On a machine with signed zeros, if the generated code generates both plus zero and minus zero, plus and minus zero must be equal by the predefined equality operators.

12
Two access-to-object values are equal if they designate the same object, or if both are equal to the null value of the access type.

13
Two access-to-subprogram values are equal if they are the result of the same evaluation of an Access attribute_reference, or if both are equal to the null value of the access type.  Two access-to-subprogram values are unequal if they designate different subprograms. [It is unspecified whether two access values that designate the same subprogram but are the result of distinct evaluations of Access attribute_references are equal or unequal.]

13.a
Reason: This allows each Access attribute_reference for a subprogram to designate a distinct "wrapper" subprogram if necessary to support an indirect call.

14
For a type extension, predefined equality is defined in terms of the primitive [(possibly user-defined)] equals operator of the parent type and of any tagged components of the extension part, and predefined equality for any other components not inherited from the parent type.

14.a
Ramification: Two values of a type extension are not equal if there is a variant_part in the extension part and the two values have different variants present. This is a ramification of the requirement that a discriminant governing such a variant_part has to be a "new" discriminant, and so has to be equal in the two values for the values to be equal.  Note that variant_parts in the parent part need not match if the primitive equals operator for the parent type considers them equal.

15
For a private type, if its full type is tagged, predefined equality is defined in terms of the primitive equals operator of the full type; if the full type is untagged, predefined equality for the private type is that of its full type.

16
For other composite types, the predefined equality operators [(and certain other predefined operations on composite types -see 4.5.1 and )] are defined in terms of the corresponding operation on matching components, defined as follows:

17 ·
For two composite objects or values of the same non-array type, matching components are those that correspond to the same component_declaration or discriminant_specification;

18 ·
For two one-dimensional arrays of the same type, matching components are those (if any) whose index values match in the following sense: the lower bounds of the index ranges are defined to match, and the successors of matching indices are defined to match;

19 ·
For two multidimensional arrays of the same type, matching components are those whose index values match in successive index positions.

20
The analogous definitions apply if the types of the two objects or values are convertible, rather than being the same.

20.a
Discussion:  Ada 83 seems to omit this part of the definition, though it is used in array type conversions.  See 4.6.

21
Given the above definition of matching components, the result of the predefined equals operator for composite types (other than for those composite types covered earlier) is defined as follows:

22 ·
If there are no components, the result is defined to be True;

23 ·
If there are unmatched components, the result is defined to be False;

24 ·
Otherwise, the result is defined in terms of the primitive equals operator for any matching tagged components, and the predefined equals for any matching untagged components.

24.a
Reason: This asymmetry between tagged and untagged components is necessary to preserve upward compatibility and corresponds with the corresponding situation with generics, where the predefined operations "reemerge" in a generic for untagged types, but do not for tagged types.  Also, only tagged types support user-defined assignment (see 7.6), so only tagged types can fully handle levels of indirection in the implementation of the type.  For untagged types, one reason for a user-defined equals operator might be to allow values with different bounds or discriminants to compare equal in certain cases. When such values are matching components, the bounds or discriminants will necessarily match anyway if the discriminants of the enclosing values match.

24.b
Ramification: Two null arrays of the same type are always equal; two null records of the same type are always equal.

24.c
Note that if a composite object has a component of a floating point type, and the floating point type has both a plus and minus zero, which are considered equal by the predefined equality, then a block compare cannot be used for the predefined composite equality. Of course, with user-defined equals operators for tagged components, a block compare breaks down anyway, so this is not the only special case that requires component-by-component comparisons. On a one's complement machine, a similar situation might occur for integer types, since one's complement machines typically have both a plus and minus (integer) zero.

25
The predefined "/=" operator gives the complementary result to the predefined "=" operator.

25.a
Ramification: Furthermore, if the user defines an "=" operator that returns Boolean, then a "/=" operator is implicitly declared in terms of the user-defined "=" operator so as to give the complementary result.  See 6.6.

26
For a discrete array type, the predefined ordering operators correspond to lexicographic order using the predefined order relation of the component type:  A null array is lexicographically less than any array having at least one component. In the case of nonnull arrays, the left operand is lexicographically less than the right operand if the first component of the left operand is less than that of the right; otherwise the left operand is lexicographically less than the right operand only if their first components are equal and the tail of the left operand is lexicographically less than that of the right (the tail consists of the remaining components beyond the first and can be null).

27
For the evaluation of a membership test, the simple_expression and the range (if any) are evaluated in an arbitrary order.

28
A membership test using in yields the result True if:

29 ·
The tested type is scalar, and the value of the simple_expression belongs to the given range, or the range of the named subtype; or

29.a
Ramification: The scalar membership test only does a range check. It does not perform any other check, such as whether a value falls in a "hole" of a "holey" enumeration type. The Pos attribute function can be used for that purpose.

29.b
Even though Standard.Float is an unconstrained subtype, the test "X in Float" will still return False (presuming the evaluation of X does not raise Constraint_Error) when X is outside Float'Range.

30 ·
The tested type is not scalar, and the value of the simple_expression satisfies any constraints of the named subtype, and, if the type of the simple_expression is class-wide, the value has a tag that identifies a type covered by the tested type.

30.a
Ramification: Note that the tag is not checked if the simple_expression is of a specific type.

31
Otherwise the test yields the result False.

32
A membership test using not in gives the complementary result to the corresponding membership test using in.

NOTES

33 13
No exception is ever raised by a membership test, by a predefined ordering operator, or by a predefined equality operator for an elementary type, but an exception can be raised by the evaluation of the operands.  A predefined equality operator for a composite type can only raise an exception if the type has a tagged part whose primitive equals operator propagates an exception.

34 14
If a composite type has components that depend on discriminants, two values of this type have matching components if and only if their discriminants are equal.  Two nonnull arrays have matching components if and only if the length of each dimension is the same for both.

Examples

35
Examples of expressions involving relational operators and membership tests:

36
X /= Y

37
"" < "A" and "A" < "Aa"     --  True
"Aa" < "B" and "A" < "A  "  --  True

38
My_Car = null               -- true if My_Car has been set to null (see 3.10.1)
My_Car = Your_Car           -- true if we both share the same car
My_Car.all = Your_Car.all   -- true if the two cars are identical

39
N not in 1 .. 10            -- range membership test
Today in Mon .. Fri         -- range membership test
Today in Weekday            -- subtype membership test (see 3.5.1)
Archive in Disk_Unit        -- subtype membership test (see 3.8.1)
Tree.all in Addition'Class  -- class membership test (see 3.9.1)

Extensions to Ada 83

39.a
Membership tests can be used to test the tag of a class-wide value.

39.b
Predefined equality for a composite type is defined in terms of the primitive equals operator for tagged components or the parent part.

Wording Changes From Ada 83

39.c
The term "membership test" refers to the relation "X in S" rather to simply the reserved word in or not in.

39.d
We use the term "equality operator" to refer to both the = (equals) and /= (not equals) operators. Ada 83 referred to = as the equality operator, and /= as the inequality operator.  The new wording is more consistent with the ISO 10646 name for "=" (equals sign) and provides a category similar to "ordering operator" to refer to both = and /=.

39.e
We have changed the term "catenate" to "concatenate".

4.5.3 Binary Adding Operators

Static Semantics

1
The binary adding operators + (addition) and - (subtraction) are predefined for every specific numeric type T with their conventional meaning. They have the following specifications:

2
function "+"(Left, Right : T) return T
function "-"(Left, Right : T) return T

3
The concatenation operators & are predefined for every nonlimited, one-dimensional array type T with component type C. They have the following specifications:

4
function "&"(Left : T; Right : T) return T
function "&"(Left : T; Right : C) return T
function "&"(Left : C; Right : T) return T
function "&"(Left : C; Right : C) return T

Dynamic Semantics

5
For the evaluation of a concatenation with result type T, if both operands are of type T, the result of the concatenation is a one-dimensional array whose length is the sum of the lengths of its operands, and whose components comprise the components of the left operand followed by the components of the right operand. If the left operand is a null array, the result of the concatenation is the right operand. Otherwise, the lower bound of the result is determined as follows:

6 ·
If the ultimate ancestor of the array type was defined by a constrained_array_definition, then the lower bound of the result is that of the index subtype;

6.a
Reason: This rule avoids Constraint_Error when using concatenation on an array type whose first subtype is constrained.

7 ·
If the ultimate ancestor of the array type was defined by an unconstrained_array_definition, then the lower bound of the result is that of the left operand.

8
[The upper bound is determined by the lower bound and the length.] A check is made that the upper bound of the result of the concatenation belongs to the range of the index subtype, unless the result is a null array. Constraint_Error is raised if this check fails.

9
If either operand is of the component type C, the result of the concatenation is given by the above rules, using in place of such an operand an array having this operand as its only component (converted to the component subtype) and having the lower bound of the index subtype of the array type as its lower bound.

9.a
Ramification: The conversion might raise Constraint_Error. The conversion provides "sliding" for the component in the case of an array-of-arrays, consistent with the normal Ada 9X rules that allow sliding during parameter passing.

10
The result of a concatenation is defined in terms of an assignment to an anonymous object, as for any function call (see 6.5).

10.a
Ramification: This implies that value adjustment is performed as appropriate - see 7.6. We don't bother saying this for other predefined operators, even though they are all function calls, because this is the only one where it matters. It is the only one that can return a value having controlled parts.

NOTES

11 15
As for all predefined operators on modular types, the binary adding operators + and - on modular types include a final reduction modulo the modulus if the result is outside the base range of the type.

11.a
Implementation Note: A full "modulus" operation need not be performed after addition or subtraction of modular types.  For binary moduli, a simple mask is sufficient.  For nonbinary moduli, a check after addition to see if the value is greater than the high bound of the base range can be followed by a conditional subtraction of the modulus. Conversely, a check after subtraction to see if a "borrow" was performed can be followed by a conditional addition of the modulus.

Examples

12
Examples of expressions involving binary adding operators:

13
Z + 0.1      --  Z has to be of a real type

14
"A" & "BCD"  --  concatenation of two string literals
'A' & "BCD"  --  concatenation of a character literal and a string literal
'A' & 'A'    --  concatenation of two character literals

Inconsistencies With Ada 83

14.a
The lower bound of the result of concatenation, for a type whose first subtype is constrained, is now that of the index subtype.  This is inconsistent with Ada 83, but generally only for Ada 83 programs that raise Constraint_Error. For example, the concatenation operator in

14.b
X : array(1..10) of Integer;
begin
X := X(6..10) & X(1..5);

14.c
would raise Constraint_Error in Ada 83 (because the bounds of the result of the concatenation would be 6..15, which is outside of 1..10), but would succeed and swap the halves of X (as expected) in Ada 9X.

Extensions to Ada 83

14.d
Concatenation is now useful for array types whose first subtype is constrained. When the result type of a concatenation is such an array type, Constraint_Error is avoided by effectively first sliding the left operand (if nonnull) so that its lower bound is that of the index subtype.

4.5.4 Unary Adding Operators

Static Semantics

1
The unary adding operators + (identity) and - (negation) are predefined for every specific numeric type T with their conventional meaning. They have the following specifications:

2
function "+"(Right : T) return T
function "-"(Right : T) return T

NOTES

3 16
For modular integer types, the unary adding operator -, when given a nonzero operand, returns the result of subtracting the value of the operand from the modulus; for a zero operand, the result is zero.

4.5.5 Multiplying Operators

Static Semantics

1
The multiplying operators * (multiplication), / (division), mod (modulus), and rem (remainder) are predefined for every specific integer type T:

2
function "*"  (Left, Right : T) return T
function "/"  (Left, Right : T) return T
function "mod"(Left, Right : T) return T
function "rem"(Left, Right : T) return T

3
Signed integer multiplication has its conventional meaning.

4
Signed integer division and remainder are defined by the relation:

5
A = (A/B)*B + (A rem B)

6
where (A rem B) has the sign of A and an absolute value less than the absolute value of B.  Signed integer division satisfies the identity:

7
(-A)/B = -(A/B) = A/(-B)

8
The signed integer modulus operator is defined such that the result of A mod B has the sign of B and an absolute value less than the absolute value of B; in addition, for some signed integer value N, this result satisfies the relation:

9
A = B*N + (A mod B)

10
[The multiplying operators on modular types are defined in terms of the corresponding signed integer operators, followed by a reduction modulo the modulus if the result is outside the base range of the type [(which is only possible for the "*" operator)].]

10.a
Ramification: The above identity satisfied by signed integer division is not satisfied by modular division because of the difference in effect of negation.

11
Multiplication and division operators are predefined for every specific floating point type T:

12
function "*"(Left, Right : T) return T
function "/"(Left, Right : T) return T

13
The following multiplication and division operators, with an operand of the predefined type Integer, are predefined for every specific fixed point type T:

14
function "*"(Left : T; Right : Integer) return T
function "*"(Left : Integer; Right : T) return T
function "/"(Left : T; Right : Integer) return T

15
[All of the above multiplying operators are usable with an operand of an appropriate universal numeric type.]  The following additional multiplying operators for root_real are predefined[, and are usable when both operands are of an appropriate universal or root numeric type, and the result is allowed to be of type root_real, as in a number_declaration]:

15.a
Ramification: These operators are analogous to the multiplying operators involving fixed or floating point types where root_real substitutes for the fixed or floating point type, and root_integer substitutes for Integer. Only values of the corresponding universal numeric types are implicitly convertible to these root numeric types, so these operators are really restricted to use with operands of a universal type, or the specified root numeric types.

16
function "*"(Left, Right : root_real) return root_real
function "/"(Left, Right : root_real) return root_real

17
function "*"(Left : root_real; Right : root_integer) return root_real
function "*"(Left : root_integer; Right : root_real) return root_real
function "/"(Left : root_real; Right : root_integer) return root_real

18
Multiplication and division between any two fixed point types are provided by the following two predefined operators:

18.a
Ramification: Universal_fixed is the universal type for the class of fixed point types, meaning that these operators take operands of any fixed point types (not necessarily the same) and return a result that is implicitly (or explicitly) convertible to any fixed point type.

19
function "*"(Left, Right : universal_fixed) return universal_fixed
function "/"(Left, Right : universal_fixed) return universal_fixed

Legality Rules

20
The above two fixed-fixed multiplying operators shall not be used in a context where the expected type for the result is itself universal_fixed - [the context has to identify some other numeric type to which the result is to be converted, either explicitly or implicitly].

20.a
Discussion:  The small of universal_fixed is infinitesimal; no loss of precision is permitted. However, fixed-fixed division is impractical to implement when an exact result is required, and multiplication will sometimes result in unanticipated overflows in such circumstances, so we require an explicit conversion to be inserted in expressions like A * B * C if A, B, and C are each of some fixed point type.

20.b
On the other hand, X := A * B; is permitted by this rule, even if X, A, and B are all of different fixed point types, since the expected type for the result of the multiplication is the type of X, which is necessarily not universal_fixed.

Dynamic Semantics

21
The multiplication and division operators for real types have their conventional meaning. [For floating point types, the accuracy of the result is determined by the precision of the result type. For decimal fixed point types, the result is truncated toward zero if the mathematical result is between two multiples of the small of the specific result type (possibly determined by context); for ordinary fixed point types, if the mathematical result is between two multiples of the small, it is unspecified which of the two is the result.]

22
The exception Constraint_Error is raised by integer division, rem, and mod if the right operand is zero. [Similarly, for a real type T with T'Machine_Overflows True, division by zero raises Constraint_Error.]

NOTES

23 17
For positive A and B, A/B is the quotient and A rem B is the remainder when A  is  divided  by  B.   The  following  relations are satisfied by the rem operator:

24
  A  rem (-B) =   A rem B
(-A) rem   B  = -(A rem B)

25 18
For any signed integer K, the following identity holds:

26
A mod B   =   (A + K*B) mod B

27
The relations between signed integer division, remainder, and modulus are illustrated by the following table:

28
A      B   A/B   A rem B  A mod B     A     B    A/B   A rem B   A mod B

29
10     5    2       0        0       -10    5    -2       0         0
11     5    2       1        1       -11    5    -2      -1         4
12     5    2       2        2       -12    5    -2      -2         3
13     5    2       3        3       -13    5    -2      -3         2
14     5    2       4        4       -14    5    -2      -4         1

30
A      B   A/B   A rem B  A mod B     A     B    A/B   A rem B   A mod B
10    -5   -2       0        0       -10   -5     2       0         0
11    -5   -2       1       -4       -11   -5     2      -1        -1
12    -5   -2       2       -3       -12   -5     2      -2        -2
13    -5   -2       3       -2       -13   -5     2      -3        -3
14    -5   -2       4       -1       -14   -5     2      -4        -4

Examples

31
Examples of expressions involving multiplying operators:

32
I : Integer := 1;
J : Integer := 2;
K : Integer := 3;

33
X : Real := 1.0;                      --     see 3.5.7
Y : Real := 2.0;

34
F : Fraction := 0.25;                 --     see 3.5.9
G : Fraction := 0.5;

35
Expression                 Value           Result Type

I*J               2         same as I and J, that is, Integer
K/J               1         same as K and J, that is, Integer
K mod J     1         same as K and J, that is, Integer

X/Y               0.5       same as X and Y, that is, Real
F/2               0.125     same as F, that is, Fraction

3*F               0.75       same as F, that is, Fraction
0.75*G             0.375     universal_fixed, implicitly convertible
                            to any fixed point type
Fraction(F*G)     0.125     Fraction, as stated by the conversion
Real(J)*Y         4.0       Real, the type of both operands after
                            conversion of J

Extensions to Ada 83

35.a
Explicit conversion of the result of multiplying or dividing two fixed point numbers is no longer required, provided the context uniquely determines some specific fixed point result type. This is to improve support for decimal fixed point, where requiring explicit conversion on every fixed-fixed multiply or divide was felt to be inappropriate.

35.b
The type universal_fixed is covered by universal_real, so real literals and fixed point operands may be multiplied or divided directly, without any explicit conversions required.

Wording Changes From Ada 83

35.c
We have used the normal syntax for function definition rather than a tabular format.

4.5.6 Highest Precedence Operators

Static Semantics

1
The highest precedence unary operator abs (absolute value) is predefined for every specific numeric type T, with the following specification:

2
function "abs"(Right : T) return T

3
The highest precedence unary operator not (logical negation) is predefined for every boolean type T, every modular type T, and for every one-dimensional array type T whose components are of a boolean type, with the following specification:

4
function "not"(Right : T) return T

5
The result of the operator not for a modular type is defined as the difference between the high bound of the base range of the type and the value of the operand.  [For a binary modulus, this corresponds to a bit-wise complement of the binary representation of the value of the operand.]

6
The operator not that applies to a one-dimensional array of boolean components yields a one-dimensional boolean array with the same bounds; each component of the result is obtained by logical negation of the corresponding component of the operand (that is, the component that has the same index value). A check is made that each component of the result belongs to the component subtype; the exception Constraint_Error is raised if this check fails.

6.a
Discussion:  The check against the component subtype is per AI-00535.

7
The highest precedence exponentiation operator ** is predefined for every specific integer type T with the following specification:

8
function "**"(Left : T; Right : Natural) return T

9
Exponentiation is also predefined for every specific floating point type as well as root_real, with the following specification (where T is root_real or the floating point type):

10
function "**"(Left : T; Right : Integer'Base) return T

11
The right operand of an exponentiation is the exponent. The expression X**N with the value of the exponent N positive is equivalent to the expression X*X*...X (with N-1 multiplications) except that the multiplications are associated in an arbitrary order.  With N equal to zero, the result is one. With the value of N negative [(only defined for a floating point operand)], the result is the reciprocal of the result using the absolute value of N as the exponent.

11.a
Ramification: The language does not specify the order of association of the multiplications inherent in an exponentiation.  For a floating point type, the accuracy of the result might depend on the particular association order chosen.

Implementation Permissions

12
The implementation of exponentiation for the case of a negative exponent is allowed to raise Constraint_Error if the intermediate result of the repeated multiplications is outside the safe range of the type, even though the final result (after taking the reciprocal) would not be. (The best machine approximation to the final result in this case would generally be 0.0.)

NOTES

13 19
As implied by the specification given above for exponentiation of an integer type, a check is made that the exponent is not negative. Constraint_Error is raised if this check fails.

Wording Changes From Ada 83

13.a
We now show the specification for "**" for integer types with a parameter subtype of Natural rather than Integer for the exponent. This reflects the fact that Constraint_Error is raised if a negative value is provided for the exponent.



[Home] [Prev] [Next] [Index]

documentation@rational.com
Copyright © 1993-1998, Rational Software Corporation.   All rights reserved.