Zum Inhalt springen

Fortran: Fortran 95: Fortran 95-Erweiterungen

Aus Wikibooks
<<< zur Fortran-Startseite
<< Fortran 95 Fortran 2003 >>
< Von der modularen zur objektorientierten Programmierung Offizielle Fortran 95-Erweiterungen >


Hier sind jene ISO/IEC-Standards angeführt, die nicht Teil des Original-Fortran 95-Standards sind, sondern erst später als offizielle Fortran 95-Erweiterungen formuliert wurden. Etliche Fortran 95-Compiler bieten diese Features derzeit noch nicht oder nur teilweise.

Floating-point Exceptions

[Bearbeiten]

Im Fortran 2003-Standard sind Module betreffend IEEE 754-Standard (IEEE Standard for Binary Floating-Point Arithmetic) enthalten. Näheres dazu wird im Fortran 2003-Kapitelabschnitt Die intrinsischen IEEE-Module beschrieben.

Allocatable Components

[Bearbeiten]

Diese Erweiterung bezieht sich auf das Schlüsselwort allocatable. In Standard-Fortran 90/95 ist die dynamische Allokation von Speicherplatz für Felder mit dem Attribut allocatable eigentlich nur in einem lokalen Kontext möglich. Der Technical Report 15581 fordert, dass solche Felder darüber hinaus auch in den Anwendungsbereichen

  • Rückgabewert von Funktionen
  • Unterprogrammparameter
  • Feldelemente in Datenverbunden

uneingeschränkt verwendbar sein sollen.

Die Enhanced Data Type Facilities werden bereits standardmäßig vom aktuellen gfortran- und ifx-Compiler unterstützt (Fortran 2003 und spätere, Stand: August 2025). Mit dem -std=f95-Compiler-Schalter funktionieren folgende Programme mit gfortran nicht.

Beispiel: Rückgabewert

Fortran 90/95-Code (free source form)
program bsp
  implicit none
    
  write ( *, * ) fun_all( 5 )
  write ( *, * ) fun_all( 7 )

! Ausgabe:
!   1 2 3 4 5
!   1 2 3 4 5 6 7

  contains
    function fun_all( n )
      implicit none
  
      integer, dimension( : ), allocatable :: fun_all
      integer, intent( in )                :: n
      integer                              :: j, st
  
      allocate( fun_all( n ), stat = st )

      if( st == 0 ) then  
        forall( j = 1 : n )
          fun_all(j) = j
        end forall	
      else	
        write( *, * ) "Allocate-Fehler"
      end if
  end function fun_all
end program bsp


Beispiel: Unterprogrammparameter

Fortran 90/95-Code (free source form)
program bsp
  implicit none
  
  integer, dimension( : ), allocatable :: dynarr
  integer                              :: j
  	  

  allocate( dynarr( 3 ) )

  forall( j = 1 : 3 )
    dynarr( j ) = j * 2
  end forall

  call fun_all( dynarr ) 
  
  write( *, * ) "Out: ", dynarr   

  deallocate( dynarr )
  allocate( dynarr( 5 ) )
  
  forall( j = 1 : 5 )
    dynarr( j ) = j * 3
  end forall
  
  call fun_all( dynarr )    

  deallocate( dynarr )


! Ausgabe:
!   Argument: 2 4 6
!   (De)Allocate im UP: 88 99
!   Out:  88 99
!   Argument: 3 6 9 12 15
!   (De)Allocate im UP: 88 99


  contains
    subroutine fun_all( a )
      implicit none
      
      integer, dimension( : ), allocatable, intent( inout ) :: a

      write( *, * ) "Argument:", a 
      
      deallocate( a )
      allocate( a (2) )
      a(1) = 88
      a(2) = 99

      write( *, * ) "(De)Allocate im UP:",  a       
   end subroutine fun_all
end program bsp

Mit Standard-Fortran 90/95 könnten sie zwar das allozierte Feld als Parameter aus dem Hauptprogramm an das Unterprogramm übergeben, dort wäre es aber nicht als allocatable kennzeichenbar und somit im Unterprogramm nicht in der gezeigten Art und Weise (de)allozierbar. Mittels Standardkonformitäts-Compileroptionen, z.B.

  • Intel Fortran Compiler: -stand
  • gfortran: -std=f95

ist überprüfbar, welche Möglichkeiten Standard-Fortran bietet und welche Eigenschaften den Erweiterungen zuschreibbar sind.


Beispiel: allocatable array im Datenverbund

Fortran 90/95-Code (free source form)
program bsp
  implicit none
  

  type struktur
    integer                              :: nr
    integer, dimension( : ), allocatable :: arr      
  end type struktur


  type( struktur ) :: a1, a2
  
  allocate( a1%arr( 5 ) )
  allocate( a2%arr( 2 ) )
  
  a1%nr     = 9453
  a1%arr(1) = 1
  a1%arr(5) = 5

  a2%nr     = 9454
  a2%arr(1) = 11
  a2%arr(2) = 22
  
  write ( *, * ) "a1 =" , a1%nr, a1%arr
  write ( *, * ) "a2 =",  a2%nr, a2%arr

! Ausgabe:
!   a1 = 9453 1 0 0 0 5
!   a2 = 9454 11 22
end program bsp



<<< zur Fortran-Startseite
<< Fortran 95 Fortran 2003 >>
< Von der modularen zur objektorientierten Programmierung Offizielle Fortran 95-Erweiterungen >