Grundgerüst Beispiel · text Kopieren 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 :: iGanzzahl real(real64) :: xdoppelte Genauigkeit complex :: zkomplex logical :: okWahrheitswert character(len=20) :: sText fester Länge real :: v(10), m(3, 3)Arrays real, allocatable :: a(:)dynamisches Array integer, parameter :: n = 10Konstante type :: name ... end typeeigener 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 nintTypen int real dble cmplx char ichar kind selected_real_kindArrays size shape sum product maxval minval maxloc minloc count any all pack merge reshape transpose matmul dot_product cshift spreadZeichen len len_trim trim adjustl adjustr index scan verify repeatZufall/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 typeVariable nicht deklariert (mit implicit none Pflicht) Error: Type mismatch in argumentfalsche Argumenttypen oder fehlendes Modul (use) Error: Unclassifiable statementSyntaxfehler, oft fehlendes then oder end Error: Expected a right parenthesisKlammern nicht ausgeglichen Program received signal SIGSEGVZugriff außerhalb von Arrays (-fcheck=bounds hilft) oder nicht allokiertes Array Fortran runtime error: End of recordEingabe/Format passt nicht Ergebnisse unterscheiden sich leicht Gleitkomma-Rundung, real statt real64 Warning: Possible change of value in conversionreal und real64 gemischt, Literal 1.0d0 oder _real64
Kompiler-Optionen (gfortran) Beispiel · bash Kopieren 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 Bibliotheken Reihenfolge 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 none immer; Module statt common-Blöcken; intent bei jedem ArgumentArray-Ausdrücke statt Schleifen; links schnellster Index real64, Toleranzvergleiche; -fcheck=all beim Entwickeln