Kurz: program haupt use name implicit none real :: y y = funktion(3.0) print *, y end program haupt ```
Teil des Kurses Fortran
Grundgerüst
module name
implicit none
private
public :: funktion
contains
function funktion(x) result(r)
real, intent(in) :: x
real :: r
r = x * 2
end function
end module name
program haupt
use name
implicit none
real :: y
y = funktion(3.0)
print *, y
end program hauptDatentypen und Deklaration
| Deklaration | Bedeutung |
|---|---|
integer :: i | Ganzzahl |
real(real64) :: x | doppelte Genauigkeit |
complex :: z | komplex |
logical :: ok | Wahrheitswert |
character(len=20) :: s | Text fester Länge |
real :: v(10), m(3, 3) | Arrays |
real, allocatable :: a(:) | dynamisches Array |
integer, parameter :: n = 10 | Konstante |
type :: name ... end type | eigener Typ |
Operatoren
+ - * / ** · == /= < <= > >= · .and. .or. .not. .eqv. · // (Strings) · % (Typkomponente) · => (Zeiger/Prozedurbindung)
Eingebaute Funktionen
| Gebiet | Funktionen |
|---|---|
| Mathematik | abs sqrt exp log log10 sin cos tan asin acos atan atan2 sinh mod modulo floor ceiling nint |
| Typen | int real dble cmplx char ichar kind selected_real_kind |
| Arrays | size shape sum product maxval minval maxloc minloc count any all pack merge reshape transpose matmul dot_product cshift spread |
| Zeichen | len len_trim trim adjustl adjustr index scan verify repeat |
| Zufall/Zeit | random_number random_seed cpu_time system_clock date_and_time |
Häufige Compilerfehler
| Meldung | Ursache |
|---|---|
Error: Symbol 'x' at (1) has no IMPLICIT type | Variable nicht deklariert (mit implicit none Pflicht) |
Error: Type mismatch in argument | falsche Argumenttypen oder fehlendes Modul (use) |
Error: Unclassifiable statement | Syntaxfehler, oft fehlendes then oder end |
Error: Expected a right parenthesis | Klammern nicht ausgeglichen |
Program received signal SIGSEGV | Zugriff außerhalb von Arrays (-fcheck=bounds hilft) oder nicht allokiertes Array |
Fortran runtime error: End of record | Eingabe/Format passt nicht |
| Ergebnisse unterscheiden sich leicht | Gleitkomma-Rundung, real statt real64 |
Warning: Possible change of value in conversion | real und real64 gemischt, Literal 1.0d0 oder _real64 |
Kompiler-Optionen (gfortran)
gfortran -O2 -Wall -Wextra -fcheck=all -g -std=f2018 programm.f90 -o programm # Entwicklung
gfortran -O3 -march=native -fopenmp programm.f90 -o programm # schnell und parallel
gfortran -c modul.f90 # nur Objektdatei und .mod
gfortran modul.o haupt.f90 -o programm -llapack -lblas # mit BibliothekenReihenfolge beachten: Module müssen vor den Dateien übersetzt werden, die sie benutzen. Build-Werkzeuge: make, CMake, fpm (fpm run, fpm test).
Weiterlernen
- fortran-lang.org (Tutorials, Paketindex, Community), Modern Fortran Explained
- Stdlib (Fortran Standard Library), BLAS/LAPACK, FFTW, netCDF
- Übungen: Project Euler, Rosetta Code (Fortran), Advent of Code
Merke
implicit noneimmer; Module stattcommon-Blöcken;intentbei jedem Argument- Array-Ausdrücke statt Schleifen; links schnellster Index
real64, Toleranzvergleiche;-fcheck=allbeim Entwickeln
Quiz zur Selbstkontrolle
Welche Compileroption aktiviert Array-Grenzprüfungen zur Laufzeit?
- -fcheck=bounds (oder -fcheck=all) (richtig)
- -O3
- -fopenmp
- -std=f2018
Wofür steht der Operator // ?
- Verkettung von Zeichenketten (richtig)
- Division
- Kommentar
- Ganzzahldivision
Was sollte in jedem Programm stehen?
- implicit none (richtig)
- common
- goto
- equivalence
Weiter im Kurs
Zurück: Numerik, Parallelrechnen und Hochleistung
Alle Kapitel: Fortran im Überblick