Fortran

Mathematical Formula Translating System

John W. Backus, IBM, 1956

International standard ISO/IEC/JTC1/SC22/WG5 NCITS/J3. Today Fortran 2008 (2010).

Fortran 77 is a subset of Fortran 90/95/2003/2008.

Getting started

Basics of using the operating system (login; copy, rename, delete, print files; etc.)

Text editor, e.g., vi, Emacs (Unix, Linux), NEdit (CIP Pool, Linux), Notepad, WinEdt (Windows)

Create text file hello.f95 (source code)

print *, "hello, world"
end

Compilation and linking, e.g., using GFortran compiler (gcc.gnu.org) yields file a.exe (executable, binary)

gfortran hello.f95

Execution

a.exe or simply a

Formula translation

Example: Celsius to Fahrenheit conversion

t/F = t/C 9/5 + 32

Fortran source code

tf = tc * 9/5 + 32

Machine code (left column) after compilation

Intel x86

000805D9     001E       fld     dword ptr .bss$+8
006C
00100DD8     0024       fmul    dword ptr .literal$+8
0002
000C35D8     002A       fdiv    dword ptr .literal$+4
0002
000805D8     0030       fadd    dword ptr .literal$
0002
00041DD9     0036       fstp    dword ptr .bss$+4
0090

.data
.literal$
42000000     0000       REAL4   32.00000
40A00000     0004       REAL4   5.000000
41100000     0008       REAL4   9.000000

DEC alpha

A41D8018     002C       ldq     r0, TC
88000000     0030       lds     f0, TC
A79D8010     0034       ldq     r28, (gp)
883C0008     0038       lds     f1, 8(r28)
2FFE0000     003C       unop
58011040     0040       muls    f0, f1, f0
A79D8010     0044       ldq     r28, (gp)
895C0004     0048       lds     f10, 4(r28)
2FFE0000     004C       unop
580A1060     0050       divs    f0, f10, f0
A79D8010     0054       ldq     r28, (gp)
897C0000     0058       lds     f11, (r28)
2FFE0000     005C       unop
580B1000     0060       adds    f0, f11, f0
A43D8018     0064       ldq     r1, TF
9801FFFC     0068       sts     f0, TF

                        .rconst
42000000     0000       .long   0x42000000 # .float 32.00000
40A00000     0004       .long   0x40A00000 # .float 5.000000
41100000     0008       .long   0x41100000 # .float 9.000000

Complete Fortran program cf.f95 (bad style, but working)

print *, "Celsius temperature?"
read *, tc
tf = tc * 9/5 + 32
print *, "Fahrenheit temperature", tf, "F"
end

Exercises

1. Replace line 5 of the above program by
a) tf = 9/5 * tc + 32
b) tf - 32 = tc * 9/5

2. Write a similar program fc.f95 to convert from Fahrenheit to Celsius.

Note 1: Contrary to real experiments in the lab, there is no risk to hurt oneself or damage the hardware by programming errors.

Note 2: GWDG charges the institutes for use of resources (CPU time, paper output, etc.). Example programs and exercises should compile and execute in less than 10 seconds. Otherwise one has to kill the process.

Note 3: The GIGO principle "Garbage in, garbage out". The computer does exactly what one tells it to do; this may not always be what one really intends.