Modern Fortran has explicit numeric types. Integer math is exact; real math approximates with floating point.

Program

Play the program to convert an integer to real and halve it.

variables.f90
Replay: real traced execution (multi-file project)
program variables
    implicit none
    integer :: count
    real :: half
    count = 5
    half = real(count) / 2.0
    print '(I0, A, F0.1)', count, " ", half
end program variables
  1. count ← 5

    4real :: half5count = 56half = real(count) / 2.0
    values this step5count
  2. half ← 2.5

    5count = 56half = real(count) / 2.07print '(I0, A, F0.1)', count, " ", half
    values this step2.5half5count
  3. print '(I0, A, F0.1)', count, " ", half

    6    half = real(count) / 2.07    print '(I0, A, F0.1)', count, " ", half8end program variables
    output5 2.5
    values this step5count2.5half

Follow the Values

  1. count is declared as an integer.
  2. half is declared as a real number.
  3. count = 5 stores the whole number 5.
  4. real(count) / 2.0 turns 5 into a real and divides it to get 2.5.
  5. The program prints 5 2.5. | name | value | role | | --- | --- | --- | | count | 5 | starting integer | | real(count) | 5.0 | real version of count | | half | 2.5 | printed real result |
type declaration `integer ::` and `real ::` give a variable its type.
type conversion `real(count)` converts an integer to a real.
format spec `F0.1` prints a real with one decimal and minimum width.

Exercise: variables.f90

Reproduce the output 5 2.5, then change count and predict the new half value before running it.