Next: Parametrized Reals Up: Parametrised Intrinsic Types Previous: Rationale   Contents

# Parametrised Integers

Consider the following declarations

```integer(kind=1) :: i1
integer(kind=2) :: i2
integer(kind=4) :: i4
integer(kind=8) :: i8
```
The variables i1, i2, i4, i8 are all of type INTEGER, but they have different KINDs; this means they will be stored using a different scheme. The presence of the numerical parameter KIND distinguishes between different kinds of integers; the exact meaning of a specific KIND parameter value is processor dependent.

For example, on an Alpha 21264 processor, i1 can hold numbers between and ; similarly, i2 can hold values between and , i3 between and , and i4 between and ; on Alpha 21264 KIND=p means storing the integer on p bytes; allowed values are p=1,2,4,8.

Numerical constants of a specific kind can be specified by appending an underscore and the kind number, for example

```i1 = 39_1;  i8 = 39876_8
```

In order to make the code completely portable, and to work with kind numbers transparently, we can specify the range of integer values. The function

```selected_int_kind(2)
```
returns the minimal kind number able to hold integers in the range . Therefore, we request the accuracy needed by the program, and let the compiler decide which kind number, on the processor at hand, satisfies this. One elegant use of the select function may look like
```integer, parameter :: short  = selected_int_kind(2), &
medium = selected_int_kind(3), &
long   = selected_int_kind(8), &
huge   = selected_int_kind(16)
integer(kind=short)  :: i1
integer(kind=medium) :: i2
integer(kind=long    :: i4
integer(kind=huge)   :: i8
i1 = 39_short;  i8 = 39876_huge
```

If there is no available kind that can accomodate the required range , the function SELECTED_INT_KIND(p) returns and we get an error at compile time.

Next: Parametrized Reals Up: Parametrised Intrinsic Types Previous: Rationale   Contents