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

3.5 Scalar Types

3.5 Scalar Types

1
Scalar types comprise enumeration types, integer types, and real types. Enumeration types and integer types are called discrete types; each value of a discrete type has a position number which is an integer value. Integer types and real types are called numeric types. All scalar types are ordered, that is, all relational operators are predefined for their values.

2
range_constraint ::= range range

range ::= range_attribute
     | simple_expression .. simple_expression

3
A range specifies a subset of values of a scalar type. The range L .. R specifies the values from L to R inclusive if the relation L <= R is true. The values L and R are called the lower bound and upper bound of the range, respectively. A value V is said to satisfy a range constraint if it belongs to the range; the value V is said to belong to the range if the relations L <= V and V <= R are both TRUE. A null range is a range for which the relation R < L is TRUE; no value belongs to a null range. The operators <= and < in the above definitions are the predefined operators of the scalar type.

4
If a range constraint is used in a subtype indication, either directly or as part of a floating or fixed point constraint, the type of the simple expressions (likewise, of the bounds of a range attribute) must be the same as the base type of the type mark of the subtype indication. A range constraint is compatible with a subtype if each bound of the range belongs to the subtype, or if the range constraint defines a null range; otherwise the range constraint is not compatible with the subtype.

5
The elaboration of a range constraint consists of the evaluation of the range. The evaluation of a range defines its lower bound and its upper bound. If simple expressions are given to specify the bounds, the evaluation of the range evaluates these simple expressions in some order that is not defined by the language.

6
Attributes

7
For any scalar type T or for any subtype T of a scalar type, the following attributes are defined:

8
T'FIRST     Yields the lower bound of T. The value of this attribute has the same type as T.

9
T'LAST     Yields the upper bound of T. The value of this attribute has the same type as T.

Note:

10
Indexing and iteration rules use values of discrete types.

11
References:

*
attribute 4.1.4

*
constraint 3.3

*
enumeration type 3.5.1

*
erroneous 1.6

*
evaluation 4.5

*
fixed point constraint 3.5.9

*
floating point constraint 3.5.7

*
index 3.6

*
integer type 3.5.4

*
loop statement 5.5

*
range attribute 3.6.2

*
real type 3.5.6

*
relational operator 4.5

*
relational operator 4.5.2

*
satisfy a constraint 3.3

*
simple expression 4.4

*
subtype indication 3.3.2

*
type mark 3.3.2

3.5.1 Enumeration Types

1
An enumeration type definition defines an enumeration type.

2
enumeration_type_definition ::=
     (enumeration_literal_specification
     {, enumeration_literal_specification})

enumeration_literal_specification ::= enumeration_literal

enumeration_literal ::= identifier | character_literal

3
The identifiers and character literals listed by an enumeration type definition must be distinct. Each enumeration literal specification is the declaration of the corresponding enumeration literal: this declaration is equivalent to the declaration of a parameterless function, the designator being the enumeration literal, and the result type being the enumeration type. The elaboration of an enumeration type definition creates an enumeration type; this elaboration includes that of every enumeration literal specification.

4
Each enumeration literal yields a different enumeration value. The predefined order relations between enumeration values follow the order of corresponding position numbers. The position number of the value of the first listed enumeration literal is zero; the position number for each other enumeration literal is one more than for its predecessor in the list.

5
If the same identifier or character literal is specified in more than one enumeration type definition, the corresponding literals are said to be overloaded. At any place where an overloaded enumeration literal occurs in the text of a program, the type of the enumeration literal must be determinable from the context (see 8.7).

6
Examples:

type DAY is (MON, TUE, WED, THU, FRI, SAT, SUN);
type SUIT is (CLUBS, DIAMONDS, HEARTS, SPADES);
type GENDER is (M, F);
type LEVEL is (LOW, MEDIUM, URGENT);
type COLOR is (WHITE, RED, YELLOW, GREEN, BLUE, BROWN, BLACK);
type LIGHT is (RED, AMBER, GREEN); -- RED and GREEN are
-- overloaded

type HEXA is (`A', `B', `C', `D', `E', `F');
type MIXED is (`A', `B', `*', B, NONE, `?', `%');

subtype WEEKDAY is DAY range MON .. FRI;
subtype MAJOR is SUIT range HEARTS .. SPADES;
subtype RAINBOW is COLOR range RED .. BLUE; -- the color RED,
-- not the light

Note:

7
If an enumeration literal occurs in a context that does not otherwise suffice to determine the type of the literal, then qualification by the name of the enumeration type is one way to resolve the ambiguity (see 8.7).

8
References:

*
character literal 2.5

*
declaration 3.1

*
designator 6.1

*
elaboration 3.9

*
elaboration 6.1

*
function 6.5

*
identifier 2.3

*
name 4.1

*
overloading 6.6

*
overloading 8.7

*
position number 3.5

*
qualified expression 4.7

*
relational operator 4.5

*
relational operator 4.5.2

*
type 3.3

*
type definition 3.3.1

3.5.2 Character Types

1
An enumeration type is said to be a character type if at least one of its enumeration literals is a character literal. The predefined type CHARACTER is a character type whose values are the 128 characters of the ASCII character set. Each of the 95 graphic characters of this character set is denoted by the corresponding character literal.

2
Example:

type ROMAN_DIGIT is (`I', `V', `X', `L', `C', `D', `M');

Notes:

3
The predefined package ASCII includes the declaration of constants denoting control characters and of constants denoting graphic characters that are not in the basic character set.

4
A conventional character set such as EBCDIC can be declared as a character type; the internal codes of the characters can be specified by an enumeration representation clause as explained in section 13.3.

5
References:

*
ascii predefined package C

*
basic character 2.1

*
character literal 2.5

*
constant 3.2.1

*
declaration 3.1

*
enumeration type 3.5.1

*
graphic character 2.1

*
identifier 2.3

*
literal 4.2

*
predefined type C

*
type 3.3

3.5.3 Boolean Types

1
There is a predefined enumeration type named BOOLEAN. It contains the two literals FALSE and TRUE ordered with the relation FALSE < TRUE. A boolean type is either the type BOOLEAN or a type that is derived, directly or indirectly, from a boolean type.

2
References:

*
derived type 3.4

*
enumeration literal 3.5.1

*
enumeration type 3.5.1

*
relational operator 4.5

*
relational operator 4.5.2

*
type 3.3

3.5.4 Integer Types

1
An integer type definition defines an integer type whose set of values includes at least those of the specified range.

2
integer_type_definition ::= range_constraint

3
If a range constraint is used as an integer type definition, each bound of the range must be defined by a static expression of some integer type, but the two bounds need not have the same integer type. (Negative bounds are allowed.)

4
A type declaration of the form:

type T is range L .. R;

5
is, by definition, equivalent to the following declarations:

type integer_type is new predefined_integer_type;
subtype T is integer_type
range integer_type(L) .. integer_type(R);

6
where integer_type is an anonymous type, and where the predefined integer type is implicitly selected by the implementation, so as to contain the values L to R inclusive. The integer type declaration is illegal if none of the predefined integer types satisfies this requirement, excepting universal_integer. The elaboration of the declaration of an integer type consists of the elaboration of the equivalent type and subtype declarations.

7
The predefined integer types include the type INTEGER. An implementation may also have predefined types such as SHORT_INTEGER and LONG_INTEGER, which have (substantially) shorter and longer ranges, respectively, than INTEGER. The range of each of these types must be symmetric about zero, excepting an extra negative value which may exist in some implementations. The base type of each of these types is the type itself.

8
Integer literals are the literals of an anonymous predefined integer type that is called universal_integer in this reference manual. Other integer types have no literals. However, for each integer type there exists an implicit conversion that converts a universal_integer value into the corresponding value (if any) of the integer type. The circumstances under which these implicit conversions are invoked are described in section 4.6.

9
The position number of an integer value is the corresponding value of the type universal_integer.

10
The same arithmetic operators are predefined for all integer types (see 4.5). The exception NUMERIC_ERROR is raised by the execution of an operation (in particular an implicit conversion) that cannot deliver the correct result (that is, if the value corresponding to the mathematical result is not a value of the integer type). However, an implementation is not required to raise the exception NUMERIC_ERROR if the operation is part of a larger expression whose result can be computed correctly, as described in section 11.6.

11
Examples:

type PAGE_NUM is range 1 .. 2;
type LINE_SIZE is range 1 .. MAX_LINE_SIZE;

subtype SMALL_INT is INTEGER range -10 .. 10;
subtype COLUMN_PTR is LINE_SIZE range 1 .. 10;
subtype BUFFER_SIZE is INTEGER range 0 .. MAX;

Notes:

12
The name declared by an integer type declaration is a subtype name. On the other hand, the predefined operators of an integer type deliver results whose range is defined by the parent predefined type; such a result need not belong to the declared subtype, in which case an attempt to assign the result to a variable of the integer subtype raises the exception CONSTRAINT_ERROR.

13
The smallest (most negative) value supported by the predefined integer types of an implementation is the named number SYSTEM.MIN_INT and the largest (most positive) value is SYSTEM.MAX_INT (see 13.7).

14
References:

*
anonymous type 3.3.1

*
belong to a subtype 3.3

*
bound of a range 3.5

*
constraint_error exception 11.1

*
conversion 4.6

*
identifier 2.3

*
integer literal 2.4

*
literal 4.2

*
numeric_error exception 11.1

*
parent type 3.4

*
predefined operator 4.5

*
range constraint 3.5

*
static expression 4.9

*
subtype declaration 3.3.2

*
system predefined package 13.7

*
type 3.3

*
type declaration 3.3.1

*
type definition 3.3.1

*
universal type 4.10

3.5.5 Operations of Discrete Types

1
The basic operations of a discrete type include the operations involved in assignment, the membership tests, and qualification; for a boolean type they include the short-circuit control forms; for an integer type they include the explicit conversion of values of other numeric types to the integer type, and the implicit conversion of values of the type universal_integer to the type.

2
Finally, for every discrete type or subtype T, the basic operations include the attributes listed below. In this presentation, T is referred to as being a subtype (the subtype T) for any property that depends on constraints imposed by T; other properties are stated in terms of the base type of T.

3
The first group of attributes yield characteristics of the subtype T. This group includes the attribute BASE (see 3.3.2), the attributes FIRST and LAST (see 3.5), the representation attribute SIZE (see 13.7.2), and the attribute WIDTH defined as follows:

4
T'WIDTH Yields the maximum image length over all values of the subtype T (the image is the sequence of characters returned by the attribute IMAGE, see below). Yields zero for a null range. The value of this attribute is of the type universal_integer.

5
All attributes of the second group are functions with a single parameter. The corresponding actual parameter is indicated below by X.

6 T'POSThis attribute is a function. The parameter X must be a value of the base type of T. The result type is the type universal_integer. The result is the position number of the value of the parameter.
7 T'VALThis attribute is a special function with a single parameter which can be of any integer type. The result type is the base type of T. The result is the value whose position number is the universal_integer value corresponding to X. The exception CONSTRAINT_ERROR is raised if the universal_integer value corresponding to X is not in the range T'POS(T'BASE'FIRST) .. T'POS(T'BASE'LAST).
8 T'SUCCThis attribute is a function. The parameter X must be a value of the base type of T. The result type is the base type of T. The result is the value whose position number is one greater than that of X. The exception CONSTRAINT_ERROR is raised if X equals T'BASE'LAST.
9 T'PREDThis attribute is a function. The parameter X must be a value of the base type of T. The result type is the base type of T. The result is the value whose position number is one less than that of X. The exception CONSTRAINT_ERROR is raised if X equals T'BASE'FIRST.
10 T'IMAGE    This attribute is a function. The parameter X must be a value of the base type of T. The result type is the predefined type STRING. The result is the image of the value of X, that is, a sequence of characters representing the value in display form. The image of an integer value is the corresponding decimal literal; without underlines, leading zeros, exponent, or trailing spaces; but with a single leading character that is either a minus sign or a space. The lower bound of the image is one.
11
The image of an enumeration value is either the corresponding identifier in upper case or the corresponding character literal (including the two apostrophes); neither leading nor trailing spaces are included. The image of a character C, other than a graphic character, is implementation-defined; the only requirement is that the image must be such that C equals CHARACTER'VALUE(CHARACTER'IMAGE(C)).
12 T'VALUE    This attribute is a function. The parameter X must be a value of the predefined type STRING. The result type is the base type of T. Any leading and any trailing spaces of the sequence of characters that corresponds to the parameter are ignored.
13
For an enumeration type, if the sequence of characters has the syntax of an enumeration literal and if this literal exists for the base type of T, the result is the corresponding enumeration value. For an integer type, if the sequence of characters has the syntax of an integer literal, with an optional single leading character that is a plus or minus sign, and if there is a corresponding value in the base type of T, the result is this value. In any other case, the exception CONSTRAINT_ERROR is raised.

14
In addition, the attributes A'SIZE and A'ADDRESS are defined for an object A of a discrete type (see 13.7.2).

15
Besides the basic operations, the operations of a discrete type include the predefined relational operators. For enumeration types, operations include enumeration literals. For boolean types, operations include the predefined unary logical negation operator not, and the predefined logical operators. For integer types, operations include the predefined arithmetic operators: these are the binary and unary adding operators - and +, all multiplying operators, the unary operator abs, and the exponentiating operator.

16
The operations of a subtype are the corresponding operations of its base type except for the following: assignment, membership tests, qualification, explicit type conversions, and the attributes of the first group; the effect of each of these operations depends on the subtype (assignments, membership tests, qualifications, and conversions involve a subtype check; attributes of the first group yield a characteristic of the subtype).

Notes:

17
For a subtype of a discrete type, the results delivered by the attributes SUCC, PRED, VAL, and VALUE need not belong to the subtype; similarly, the actual parameters of the attributes POS, SUCC, PRED, and IMAGE need not belong to the subtype. The following relations are satisfied (in the absence of an exception) by these attributes:

T'POS(T'SUCC(X)) = T'POS(X) + 1
T'POS(T'PRED(X)) = T'POS(X) - 1

T'VAL(T'POS(X)) = X
T'POS(T'VAL(N)) = N

18
Examples:

-- For the types and subtypes declared in section 3.5.1
-- we have:

-- COLOR'FIRST = WHITE, COLOR'LAST = BLACK
-- RAINBOW'FIRST = RED, RAINBOW'LAST = BLUE

-- COLOR'SUCC(BLUE) = RAINBOW'SUCC(BLUE) = BROWN
-- COLOR'POS(BLUE) = RAINBOW'POS(BLUE) = 4
-- COLOR'VAL(0) = RAINBOW'VAL(0) = WHITE

19
References:

*
abs operator 4.5

*
abs operator 4.5.6

*
assignment 5.2

*
attribute 4.1.4

*
base type 3.3

*
basic operation 3.3.3

*
binary adding operator 4.5

*
binary adding operator 4.5.3

*
boolean type 3.5.3

*
bound of a range 3.5

*
character literal 2.5

*
constraint 3.3

*
constraint_error exception 11.1

*
conversion 4.6

*
discrete type 3.5

*
enumeration literal 3.5.1

*
exponentiating operator 4.5

*
exponentiating operator 4.5.6

*
function 6.5

*
graphic character 2.1

*
identifier 2.3

*
integer type 3.5.4

*
logical operator 4.5

*
logical operator 4.5.1

*
membership test 4.5

*
membership test 4.5.2

*
multiplying operator 4.5

*
multiplying operator 4.5.5

*
not operator 4.5

*
not operator 4.5.6

*
numeric literal 2.4

*
numeric type 3.5

*
object 3.2

*
operation 3.3

*
position number 3.5

*
predefined operator 4.5

*
predefined type C

*
qualified expression 4.7

*
relational operator 4.5

*
relational operator 4.5.2

*
short-circuit control form 4.5

*
short-circuit control form 4.5.1

*
string type 3.6.3

*
subtype 3.3

*
type 3.3

*
unary adding operator 4.5

*
unary adding operator 4.5.4

*
universal_integer type 3.5.4

*
universal type 4.10

3.5.6 Real Types

1
Real types provide approximations to the real numbers, with relative bounds on errors for floating point types, and with absolute bounds for fixed point types.

2
real_type_definition ::=
     floating_point_constraint | fixed_point_constraint

3
A set of numbers called model numbers is associated with each real type. Error bounds on the predefined operations are given in terms of the model numbers. An implementation of the type must include at least these model numbers and represent them exactly.

4
An implementation-dependent set of numbers, called the safe numbers, is also associated with each real type. The set of safe numbers of a real type must include at least the set of model numbers of the type. The range of safe numbers is allowed to be larger than the range of model numbers, but error bounds on the predefined operations for safe numbers are given by the same rules as for model numbers. Safe numbers therefore provide guaranteed error bounds for operations on an implementation-dependent range of numbers; in contrast, the range of model numbers depends only on the real type definition and is therefore independent of the implementation.

5
Real literals are the literals of an anonymous predefined real type that is called universal_real in this reference manual. Other real types have no literals. However, for each real type, there exists an implicit conversion that converts a universal_real value into a value of the real type. The conditions under which these implicit conversions are invoked are described in section 4.6. If the universal_real value is a safe number, the implicit conversion delivers the corresponding value; if it belongs to the range of safe numbers but is not a safe number, then the converted value can be any value within the range defined by the safe numbers next above and below the universal_real value.

6
The execution of an operation that yields a value of a real type may raise the exception NUMERIC_ERROR, as explained in section 4.5.7, if it cannot deliver a correct result (that is, if the value corresponding to one of the possible mathematical results does not belong to the range of safe numbers); in particular, this exception can be raised by an implicit conversion. However, an implementation is not required to raise the exception NUMERIC_ERROR if the operation is part of a larger expression whose result can be computed correctly (see 11.6).

7
The elaboration of a real type definition includes the elaboration of the floating or fixed point constraint and creates a real type.

Note:

8
An algorithm written to rely only upon the minimum numerical properties guaranteed by the type definition for model numbers will be portable without further precautions.

9
References:

*
conversion 4.6

*
elaboration 3.9

*
fixed point constraint 3.5.9

*
floating point constraint 3.5.7

*
literal 4.2

*
numeric_error exception 11.1

*
predefined operation 3.3.3

*
real literal 2.4

*
type 3.3

*
type definition 3.3.1

*
universal type 4.10

3.5.7 Floating Point Types

1
For floating point types, the error bound is specified as a relative precision by giving the required minimum number of significant decimal digits.

2
floating_point_constraint ::=
     floating_accuracy_definition [range_constraint]

floating_accuracy_definition ::=
     digits static_simple_expression

3
The minimum number of significant decimal digits is specified by the value of the static simple expression of the floating accuracy definition. This value must belong to some integer type and must be positive (nonzero); it is denoted by D in the remainder of this section. If the floating point constraint is used as a real type definition and includes a range constraint, then each bound of the range must be defined by a static expression of some real type, but the two bounds need not have the same real type.

4
For a given radix, the following canonical form is defined for any floating point model number other than zero:

sign * mantissa * (radix ** exponent)

5
In this form: sign is either +1 or -1; mantissa is expressed in a number base given by radix; and exponent is an integer number (possibly negative) such that the integer part of mantissa is zero and the first digit of its fractional part is not a zero.

6
The specified number D is the minimum number of decimal digits required after the point in the decimal mantissa (that is, if radix is ten). The value of D in turn determines a corresponding number B that is the minimum number of binary digits required after the point in the binary mantissa (that is, if radix is two). The number B associated with D is the smallest value such that the relative precision of the binary form is no less than that specified for the decimal form. (The number B is the integer next above (D*log(10)/log(2)) + 1.)

7
The model numbers defined by a floating accuracy definition comprise zero and all numbers whose binary canonical form has exactly B digits after the point in the mantissa and an exponent in the range -4*B .. +4*B. The guaranteed minimum accuracy of operations of a floating point type is defined in terms of the model numbers of the floating point constraint that forms the corresponding real type definition (see 4.5.7).

8
The predefined floating point types include the type FLOAT. An implementation may also have predefined types such as SHORT_FLOAT and LONG_FLOAT, which have (substantially) less and more accuracy, respectively, than FLOAT. The base type of each predefined floating point type is the type itself. The model numbers of each predefined floating point type are defined in terms of the number D of decimal digits returned by the attribute DIGITS (see 3.5.8).

9
For each predefined floating point type (consequently also for each type derived therefrom), a set of safe numbers is defined as follows. The safe numbers have the same number B of mantissa digits as the model numbers of the type and have an exponent in the range -E .. +E where E is implementation-defined and at least equal to the 4*B of model numbers. (Consequently, the safe numbers include the model numbers.) The rules defining the accuracy of operations with model and safe numbers are given in section 4.5.7. The safe numbers of a subtype are those of its base type.

10
A floating point type declaration of one of the two forms (that is, with or without the optional range constraint indicated by the square brackets):

type T is digits D [range L .. R];

11
is, by definition, equivalent to the following declarations:

type floating_point_type is new predefined_floating_point_type;
subtype T is floating_point_type digits D
[range floating_point_type(L) .. floating_point_type(R)];

12
where floating_point_type is an anonymous type, and where the predefined floating point type is implicitly selected by the implementation so that its model numbers include the model numbers defined by D; furthermore, if a range L .. R is supplied, then both L and R must belong to the range of safe numbers. The floating point declaration is illegal if none of the predefined floating point types satisfies these requirements, excepting universal_real. The maximum number of digits that can be specified in a floating accuracy definition is given by the system-dependent named number SYSTEM.MAX_DIGITS (see 13.7.1).

13
The elaboration of a floating point type declaration consists of the elaboration of the equivalent type and subtype declarations.

14
If a floating point constraint follows a type mark in a subtype indication, the type mark must denote a floating point type or subtype. The floating point constraint is compatible with the type mark only if the number D specified in the floating accuracy definition is not greater than the corresponding number D for the type or subtype denoted by the type mark. Furthermore, if the floating point constraint includes a range constraint, the floating point constraint is compatible with the type mark only if the range constraint is, itself, compatible with the type mark.

15
The elaboration of such a subtype indication includes the elaboration of the range constraint, if there is one; it creates a floating point subtype whose model numbers are defined by the corresponding floating accuracy definition. A value of a floating point type belongs to a floating point subtype if and only if it belongs to the range defined by the subtype.

16
The same arithmetic operators are predefined for all floating point types (see 4.5).

Notes:

17
A range constraint is allowed in a floating point subtype indication, either directly after the type mark, or as part of a floating point constraint. In either case the bounds of the range must belong to the base type of the type mark (see 3.5). The imposition of a floating point constraint on a type mark in a subtype indication cannot reduce the allowed range of values unless it includes a range constraint (the range of model numbers that correspond to the specified number of digits can be smaller than the range of numbers of the type mark). A value that belongs to a floating point subtype need not be a model number of the subtype.

18
Examples:

type COEFFICIENT is digits 10 range -1.0 .. 1.0;

type REAL is digits 8;
type MASS is digits 7 range 0.0 .. 1.0E35;

subtype SHORT_COEFF is COEFFICIENT digits 5;
-- a subtype with less accuracy
subtype PROBABILITY is REAL range 0.0 .. 1.0;
-- a subtype with a smaller range

Notes on the examples:

19
The implemented accuracy for COEFFICIENT is that of a predefined type having at least 10 digits of precision. Consequently the specification of 5 digits of precision for the subtype SHORT_COEFF is allowed. The largest model number for the type MASS is approximately 1.27E30 and hence less than the specified upper bound (1.0E35). Consequently the declaration of this type is legal only if this upper bound is in the range of the safe numbers of a predefined floating point type having at least 7 digits of precision.

20
References:

*
anonymous type 3.3.1

*
arithmetic operator 3.5.5

*
arithmetic operator 4.5

*
based literal 2.4.2

*
belong to a subtype 3.3

*
bound of a range 3.5

*
compatible 3.3.2

*
derived type 3.4

*
digit 2.1

*
elaboration 3.1

*
elaboration 3.9

*
error bound 3.5.6

*
exponent 2.4.1

*
integer type 3.5.4

*
model number 3.5.6

*
operation 3.3

*
predefined operator 4.5

*
predefined type C

*
range constraint 3.5

*
real type 3.5.6

*
real type definition 3.5.6

*
safe number 3.5.6

*
simple expression 4.4

*
static expression 4.9

*
subtype declaration 3.3.2

*
subtype indication 3.3.2

*
subtype 3.3

*
type 3.3

*
type declaration 3.3.1

*
type mark 3.3.2



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

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