 |
» |
|
|
 |
The intrinsic data types are the data types predefined by
the HP Fortran 90 language, in contrast with derived
types, which are user-defined (see “Derived types”). The intrinsic data types include numeric
types: and nonnumeric types: Each type allows the specification of a kind parameter to
select a data representation for that type (see “Type declaration for intrinsic types” for the format
of the kind parameter). If the kind parameter is not specified,
each type has a default data representation. Table 3-1 “Intrinsic data types” identifies the data representation for
each type, including the default case where a kind parameter is
not specified. The types are listed by keyword and applicable kind
parameter. The table also includes the data representation for the
HP extensions, BYTE and DOUBLE COMPLEX. As shown in Table 3-1 “Intrinsic data types”, HP Fortran 90
aligns data on natural boundaries. Entities of the intrinsic data
types are aligned in memory on byte boundaries of 1, 2, 4, or 8,
depending on their size. Array variables are aligned on an address
that is a multiple of the alignment required for the scalar variable
with the same type and kind parameters.  |  |  |  |  | NOTE: The ASCII character set uses only the values 0 to 127
(7 bits), but the HP Fortran 90 implementation
allows use of all 8 bits of a character entity. The processing of
character sets requiring multibyte representation for each character
makes use of all 8 bits. |  |  |  |  |
For additional information about data representation models,
see “Data representation
models”. Table 3-1 Intrinsic data types Type | Range of values | Precision (in
decimal digits) | Bytes | Alignment | INTEGER(1) | -128 to 127 | Not applicable | 1 | 1 | INTEGER(2) | -215 to 215-1 | Not applicable | 2 | 2 | INTEGER(4)
(default) | -231 to 231-1 | Not applicable | 4 | 4 | INTEGER(8) | -263 to 263-1 | Not applicable | 8 | 8 | REAL(4)
(default) | -3.402823x1038
to -1.175495x10-38 0.0 +1.175495x10-38
to +3.402823x1038 | 6 to 9 | 4 | 4 | REAL(8) | -1.797693x10+308
to -2.225073x10-308 0.0 +2.225073x10-308
to +1.797693x10+308 | 15 to 17 | 8 | 8 | REAL(16) | -1.189731x10+4932
to -3.362103x10-4932 0.0 +3.362103x10-4932
to +1.189731x10+4932 | 33 to 35 | 16 | 8 | DOUBLE PRECISION | Same as for REAL(8) | 15 to 17 | 8 | 8 | COMPLEX(4) | Same as for REAL(4) | Same as for
REAL(4) | 8 | 4 | COMPLEX(8) | Same as for REAL(8) | Same as for
REAL(8) | 16 | 8 | DOUBLE COMPLEX | Same as for REAL(8) | Same as for
REAL(8) | 16 | 8 | CHARACTER(1)
(default) | ASCII character set | Not applicable | 1 | 1 | LOGICAL(1) | .TRUE.
and .FALSE. | Not applicable | 1 | 1 | LOGICAL(2) | .TRUE.
and .FALSE. | Not applicable | 2 | 2 | LOGICAL(4) (default) | .TRUE.
and .FALSE. | Not applicable | 4 | 4 | LOGICAL(8) | .TRUE.
and .FALSE. | Not applicable | 8 | 8 |
Type declaration for intrinsic types |  |
The following is the general form of a type declaration statement
for the intrinsic data types: type-spec[[,attribute-spec] ... :: ] entity-list - type-spec
is one of : DOUBLE PRECISION
[kind-selector] CHARACTER
[char-selector]
BYTE and DOUBLE COMPLEX
are HP extensions. BYTE is equivalent
to INTEGER(KIND=1).
DOUBLE PRECISION
is equivalent to REAL(KIND=8),
and DOUBLE COMPLEX
is equivalent to COMPLEX(KIND=8),
except when +autodbl
or +autodbl4
is used. Refer to the HP Fortran Programmer's Guide
for information about using these options to increase sizes. Refer
to Chapter 10 “HP Fortran 90 statements” for information
about each type-spec. If type-spec is present, it overrides
the implicit-typing rules; see “Implicit typing”. As an HP extension to the Standard, type-spec
can also take the form: type*length where type is an
intrinsic type excluding BYTE,
CHARACTER, DOUBLE COMPLEX,
and DOUBLE PRECISION; and length
is the number of bytes of storage required, as shown in Table 3-1 “Intrinsic data types”. Alternatively, *length
may appear after the entity name. If the entity is an array with
an array specification following it, *length
may appear after the array specification. If *length
appears with the entity name, it overrides the length specified
by kind-selector. - kind-selector
is ([KIND=]scalar-int-init-expr) - scalar-int-init-expr
is a scalar integer initialization expression that
must evaluate to one of the kind parameters available (see Table 3-1 “Intrinsic data types”). For information about
initialization expressions, see “Initialization expressions”. - char-selector
specifies the length and kind of the character variable,
when type-spec is CHARACTER;
see “CHARACTER” for details. - attribute-spec
is one or more of the attributes listed in Table 3-2 “Attributes
in type declaration statement”. Some attributes are incompatible
with others; for information about which attributes are compatible
as well as full descriptions of all the attributes, see Chapter 10 “HP Fortran 90 statements”. - entity-list
is a comma-separated list of entity names of the
form: var-name
[(array-spec)] [*char-len] [
= init-expr] function-name[(array-spec
)] [*char-len]
wherearray-spec is described in
“Array declarations”; char-len
is described with the CHARACTER
statement in Chapter 10; and init-expr
is described in “Initialization expressions”.
If you include init-expr in entity,
you must also include the double colon (::)
separator. As an extension to the Standard, HP Fortran 90
permits the use of slashes to delimit init-expr.
The double colon separator, array constructors, and structure constructors
are not allowed in this form of initialization. Arrays may be initialized
by defining a list of values that are sequence associated with the
elements of the array.
Table 3-2 Attributes
in type declaration statement Attribute | Description |
|---|
AUTOMATIC | Makes procedure variables automatic (extension). | ALLOCATABLE | Declares an array that can be allocated
during execution. | DIMENSION(array-spec) | Declares an array; see “Array declarations”. If entity-list
also includes an array-spec, it overrides
the DIMENSION attribute. | EXTERNAL | Specifies a subprogram or block data
located in another program unit. | INTENT | Defines the mode of use of a dummy argument. | INTRINSIC | Allows a specific intrinsic name as an
actual argument. | OPTIONAL | Declares the presence of an actual argument
as optional. | PARAMETER | Defines named constants. | POINTER | Declares the entity to be a pointer. | PRIVATE | Inhibits visibility outside a module. | PUBLIC | Provides visibility outside a module. | SAVE | Ensures the entity retains its value
between calls of a procedure. | STATIC | Ensures the entity retains its value
between calls of a procedure (extension). | TARGET | Enables the entity to be the target of
a pointer. | VOLATILE | Provides for data sharing between asynchronous
processes (extension). |
The following are examples of type declaration statements: ! Default, KIND=4, integers i j k. INTEGER i, j, k |
! Using optional separator. INTEGER :: i,j,k |
! An 8-byte initialized integer. INTEGER(KIND=8) :: i=2**40 |
! 10 element array of 8-byte integers. INTEGER(8),DIMENSION(10) :: i |
! Using an array constructor for initialization. REAL, DIMENSION(2,2):: a = RESHAPE((/1.,2.,3.,4./),(/2,2/)) |
! Initialized complex. COMPLEX :: z=(1.0,2.0) |
! SYNTAX ERROR - no :: present. COMPLEX z=(1.0,2.0) ! ILLEGAL |
! Initialization using the HP slash extension INTEGER i/1/,j/2/ REAL a(2,2)/1.1,2.1,1.2,2.2/ ! a(i,j)=i.j |
! One character (default length). CHARACTER(KIND=1) :: c |
! A 10-byte character string. CHARACTER(LEN=10) :: c |
! Length can be * for a named constant; title is a 13-byte ! character string CHARACTER(*),PARAMETER :: title='Ftn 90 MANUAL' |
! next four declarations are all equivalent, but only the last ! is standard-conforming REAL*8 r8(10) REAL r8*8(10) REAL r8(10)*8 REAL(8), DIMENSION(10) :: r8 |
! If the statement is in a subprogram, n must be known at entry; ! otherwise, it must be a constant. CHARACTER(LEN=n) :: c SUBROUTINE x(c) CHARACTER*(*) :: c ! c assumes the length of the actual argument. END |
! A single entity, of derived type node. TYPE(node):: list_element |
! Declaration and initialization of a user-defined variable TYPE(coord) :: origin = coord(0.0,0.0) |
Implicit typing |  |
In Fortran 90, an entity may be used without having
been declared in a type declaration statement. The compiler determines
the type of the entity by applying implicit typing rules. The default
implicit typing rules are: Names with initial letter A
to H or O
to Z are of type
real. Names with initial letter I
to N are of type
integer.
Because Fortran 90 is a case-insensitive language,
the same rules apply to both uppercase and lowercase letters. The following statements DIMENSION a(5), i(10) k = 1 b = k
|
implicitly declare a
and b as default
reals and i and
k as default
integers. As described in Chapter 10, the IMPLICIT
statement enables you to change or cancel the default implicit typing
rules. The IMPLICIT statement takes
effect for the scoping unit in which it appears, except where overridden
by explicit type statements. You can override the implicit typing rules and enforce explicit
typing—that is, declare entities in type declaration statements—with
the IMPLICIT NONE
statement. If this statement is included in a scoping unit, all
names in that unit must have their types explicitly declared. You
can also enforce explicit typing for all names within a source file
by compiling with the +implicit_none
option. This option has the effect of including an IMPLICIT NONE
statement in every program unit within a source file. For a full description of the IMPLICIT
and IMPLICIT NONE
statements, see Chapter 10 “HP Fortran 90 statements”.
The +implicit_none
option is described in the HP Fortran 90 Programmer's
Guide. Constants |  |
Constants can be either literal or named. A literal
constant is a sequence of characters that represents
a value. A named constant is a variable that
has been initialized and has the PARAMETER
attribute. This section describes the formats of literal constants
for each of the intrinsic data types. For more information about
named constants and the PARAMETER
statement and attribute, see Chapter 10. The format of a signed integer literal constant is: [sign] digit-string [_kind-parameter] - sign
is either +
or -. - digit-string
takes the form: digit[digit]
... - kind-parameter
is one of: the name of a scalar integer constant
The following are examples of integer constants: In the last example, ILEN
is a named integer constant used as a kind parameter. It must have
a value of 1, 2, 4, or 8. Fortran 90 allows DATA
statements to include constants that are formatted in binary, octal,
or hexadecimal base. Such constants are called BOZ constants. A binary constant is: leading-letter{'digit-string'|"digit-string"}where leading-letter is the single
character B, O,
or Z, indicating binary, octal,
or hexadecimal base, respectively. digit-string
must consist of digits that can represent the base, namely: Hexadecimal: 0
through 9, and A
through F. The letters can be uppercase
or lowercase.
In the following, the three DATA
statements use BOZ constants to initialize i,
j, and k
to the decimal value 74: INTEGER i, j, k DATA i/B"01001010"/ DATA j/O"112"/ DATA k/Z"4A"/ |
As an extension, HP Fortran 90 allows octal
constants with a trailing O,
and hexadecimal constants with a trailing X.
The following DATA statements initialize
j and k
to the decimal value 74: DATA j/"112"O/ DATA k/"4A"X/ |
HP Fortran 90 also allows the use of BOZ
constants in contexts other than the DATA
statement; see “Typeless constants”. Hollerith constants have the form: lenHstring where len is the number of characters
in the constant and string contains exactly
len characters. The value of the constant
is the value of the pattern of bytes generated by the ASCII values
of the characters. As an extension, HP Fortran 90 allows Hollerith
constants to appear in the same contexts as BOZ constants (see “Typeless constants”), as well as wherever
a character string is valid. If len is
greater than the number of characters in string,
the constant is padded on the right with space characters. If len
is less than the number of characters in string,
the constant is truncated on the right. If a Hollerith constant appears as an argument to the conversion
functions INT
and LOGICAL,
the kind parameter is KIND=1 if
the length of the constant is 1 byte, KIND=2
if the length is 2 bytes, KIND=4
if 3 0r 4 bytes, and KIND=8 if
greater than 4. Following are examples of Hollerith constants: 5HABCbb !bb = two space characters, making the length equal to 5 |
HP Fortran 90 extends the uses of binary,
octal, and hexadecimal constants (BOZ) beyond those prescribed in
the Fortran 90 Standard; see “BOZ constants”. HP Fortran 90 allows
BOZ constants to be used as typeless constants
wherever an intrinsic literal constant of any numeric or logical
type is permitted. If possible, the type attached to a typeless constant is derived
from the magnitude of the constant and the context in which it appears.
When used as one operand of a binary operator, it assumes the type
of the other operand. If it is used as the right-hand side of an
assignment, the type of the object on the left-hand side is assumed.
When used to define the value within a structure constructor, it
assumes the type of the corresponding component. If appearing in
an array constructor, it assumes the type of the first element of
the constructor. The following rules and restrictions also apply: If the context does not determine
the type, a warning is issued and the type attached to the constant
is: INTEGER(4)
if the constant occupies 1-4 bytes. INTEGER(8)
if the constant occupies more than 4 bytes.
Leading zeros are considered significant in determining the
size. For example, Z'00000001'
assumes INTEGER(4),
and Z'000000001'
assumes INTEGER(8). The compiler truncates and issues a warning if more
than 8 bytes are required to represent a constant—for example,
Z'12345678123456781234'.
The resulting truncated value differs from that specified in the
source code. When the size of the type determined by context
does not match the size of the actual constant, the constant is
either extended with zeroes on the left or truncated from the left
as necessary. If a single constant is assigned to a complex entity,
it is assumed to represent the real part only and will assume the
real type with the same length as the complex entity. When the compiler attempts to resolve a generic
procedure, a BOZ constant in the argument list is considered to
match a logical or numeric dummy argument. An ambiguous reference
is likely to occur. See “Generic procedures” for information about generic procedures. Except for the intrinsic conversion procedures,
a BOZ constant used as an actual argument for an intrinsic procedure
assumes the integer type. The intrinsic functions INT,
LOGICAL, REAL,
DBLE, DREAL,
CMPLX, and DCMPLX
are available to force a BOZ constant to a specific type. If a BOZ
constant is specified as an argument to these functions, its assumed
type is determined as follows: For functions INT
and LOGICAL the
assumed type will be (respectively) INTEGER(KIND=4)
and LOGICAL(KIND=4),
if the constant occupies 1 to 4 bytes; otherwise, the type is assumed
to be INTEGER(KIND=8)
and LOGICAL(KIND=8). For the functions REAL,
DBLE, DREAL,
CMPLX, and DCMPLX
an argument of type REAL(KIND=4)
is assumed if the constant occupies 1 to 4 bytes, REAL(KIND=8)
if it occupies 5 to 8 bytes, and REAL(KIND=16)
otherwise.
The following examples illustrate the extended use of BOZ
constants: ! The value is 20 (constant treated as INTEGER(2) and ! truncated on the left). 10_2 + Z"1000A" |
LOGICAL(2) :: lgl2 ! Constant treated as LOGICAL(2), the type of the variable. lgl2 = B"1" |
! Constant treated as INTEGER(4); IABS is used. ABS(Z"41") |
! Constant treated as REAL(8) as it is more than 4 bytes. REAL(Z"3FF0000000000000") |
A signed real literal constant is one of: [sign]digit-string[[.[digit-string]]][exponent][_kind-parameter] - exponent
takes the form: exponent-letter
[sign] digit-string - exponent-letter
is the character E,
D, or Q.
Q is an HP Fortran 90
extension.
sign and digit-string are explained in “Integer constants”. If no kind parameter is present, or if the exponent
letter E
is present, the default kind representation is used; see Table 3-1 “Intrinsic data types”. If the exponent
letter is D,
the kind parameter is 8, and if the exponent letter
is Q, the kind
parameter is 16. If both an exponent
and a kind parameter are specified, the exponent letter
must be E. Following are examples of real constants: 1.234_8 !1.234 with approximately 15 digits precision |
-2.53Q-300 !-2.53 x 10 to the -300th, with approximately 34 ! digits precision |
A complex literal constant has the form: (real-part, imaginary-part) - real-part
and imaginary-part
are each one of: signed-integer-literal-constant signed-real-literal-constant
The kind parameter of the complex value corresponds to the
kind parameter of the part with the larger storage requirement. Following are examples of complex constants: (1.0E2, 2.3E-2) !default complex value |
(3.0_8,4.2_4) !complex value with KIND=8 |
A character literal constant is one of: [kind-parameter_]'character-string' [kind-parameter_]"character-string" The delimiting characters are not part of the constant. If
you need to place a single quote in a string delimited by single
quotes, use two single quotes; the same rule applies for double
quotes. Following are examples of character constants: "Bach""s Preludes" ! actual constant is: Bach"s Preludes |
"" ! a zero length constant |
For compatibility with C-language syntax, HP Fortran 90
allows the backslash character (\) as an escape character
in character strings. You must use the +escape
option to enable this feature. When this option is enabled, the
compiler ignores the backslash character and either substitutes
an alternative value for the character following, or interprets
the character as a quoted value. For example: is a valid string when compiled with the +escape
option. The backslash is not counted in the length of the string.
Also, if \&
appears at the end of a line when the +escape
option is enabled, the ampersand is not treated as a continuation
indicator. Table 3-3 “Escape characters” lists recognized
escape sequences. Table 3-3 Escape characters Escape character | Effect |
|---|
\n | Newline | \t | Horizontal
tab | \v | Vertical
tab | \b | Backspace | \f | Form
feed | \0 | Null | \' | Apostrophe
(does not terminate a string) | \" | Double
quote (does not terminate a string) | \\ | \ | \x | x,
where x is any character other than 1 |
The format of a logical literal constant is: {.TRUE.|.FALSE.}[_kind-parameter]The following are examples of logical constants: In standard-conforming programs, a logical value of .TRUE.
is represented by 1, and .FALSE.
is represented by 0. In nonstandard-conforming programs involving
arithmetic operators with logical operands, a logical variable may
be assigned a value other than 0 or 1. In this case, any nonzero
value is .TRUE.,
and only the value zero is .FALSE. Character substrings |  |
A character substring is a contiguous subset of a character
string. The substring is defined by the character positions of its
start and end within the string, formatted as follows: string ([ starting-position ] : [ ending-position ]) - starting-position
is a scalar expression. If starting-position
is omitted, a value of 1 is assumed. The starting-position
must be greater than or equal to 1, unless the substring has zero
length. - ending-position
is a scalar integer expression. If ending-position
is omitted, the value of the length of the character string is assumed.
The length of the substring is: MAX (ending-position - starting-position + 1, 0) The following example, substring.f90, illustrates the basic
operation on a substring. Example 3-1 substring.f90 PROGRAM main CHARACTER(LEN=15) :: city_name city_name = "CopXXXagen" PRINT *, "The city"s name is: ", city_name city_name(4:6) = "enh" ! assign to a substring of city_name PRINT *, "The city"s name is: ", city_name END PROGRAM main |
Here are the command lines to compile and execute the program,
along with the output from a sample run: $ f90 substring.f90 $ a.out The city's name is: CopXXXagen The city's name is: Copenhagen
|
For information about substring operations on an array of
strings, see “Array sections”. Character strings as
automatic data objects |  |
An automatic data object can be either an automatic array
(see “Explicit-shape arrays”) or a character
string that is local to a subprogram and whose size is nonconstant.
The size of a character string is determined when the subprogram
is called and can vary from call to call. An automatic character string must not be: Declared with the SAVE
attribute Initialized in a type declaration statement or DATA
statement
The following example, swap_names.f90, illustrates
the use of automatic character strings: Example 3-2 swap_names.f90 PROGRAM main ! actual arguments to pass to swap_names CHARACTER(6) :: n1 = "George", n2 = "Martha" CHARACTER(4) :: n3 = "pork", n4 = "salt" PRINT *, "Before: n1 = ", n1, " n2 = ", n2 CALL swap_names(n1, n2) PRINT *, "After: n1 = ", n1, " n2 = ", n2 PRINT *, "Before: n3 = ", n3, " n4 = ", n4 CALL swap_names(n3, n4) PRINT *, "After: n3 = ", n3, " n4 = ", n4 END PROGRAM main ! swap the arguments - two character strings of the same length SUBROUTINE swap_names (name1, name2) CHARACTER(*) :: name1, name2 ! the arguments ! declare another character string, temp, to be used in the ! exchange. temp is an automatic data object, its length ! can vary from call to call CHARACTER(LEN(name1)) :: temp ! the exchange temp = name1 name1 = name2 name2 = temp END SUBROUTINE swap_names |
Here are the command lines to compile and execute the program,
along with the output from a sample run: $ f90 swap_names.f90 $ a.out Before: n1 = George n2 = Martha After: n1 = Martha n2 = George Before: n3 = pork n4 = salt After: n3 = salt n4 = pork
|
|