型と種類#

これらの組み込み関数を使用すると、ある型の変数を別の型に明示的にキャストしたり、ポリモーフィック変数を使用する場合に、変数型に基づいてコードブロックを条件付きで実行したりできます。

Fortran のデータ型#

Fortran には、5 つの基本的な組み込みデータ型があります。

  • 整数型

    整数型は、整数値のみを保持できます。

  • 実数型

    2.0、3.1415、-100.876 など、浮動小数点数を格納します。

  • 複素数型

    複素数には、実数部と虚数部の 2 つの部分があります。2 つの連続する浮動小数点ストレージユニットに 2 つの部分が格納されます。

  • 論理型

    論理値は、.true. と .false. の 2 つのみです。

  • 文字型

    文字型は文字列を格納します。文字列の長さは、len 指定子で指定できます。長さが指定されていない場合は 1 です。

これらの「型」には、多くの「種類」があります。多くの場合、異なる数値の種類は異なるストレージサイズを占めるため、異なる範囲を表すことができます。しかし、異なる種類は他の意味を持つ可能性があります。たとえば、文字変数は、ASCII 文字、UTF-8 文字、または Unicode 文字を表す場合があります。

これらの基本型から独自のデータ型を派生させることもできます。

暗黙の型付け#

Fortran では、暗黙の型付けと呼ばれる機能を使用できます。つまり、一部の変数を事前に宣言する必要はありません。デフォルトでは、変数が宣言されていない場合、その名前の最初の文字によって型が決まります。

  1. i-n(「integer」の最初の 2 文字)で始まる変数名は、integer 変数を指定します。

  2. 他のすべての変数名は、デフォルトで実数になります。

ただし、ほとんどの環境では、すべての変数を宣言することが適切なプログラミング方法と見なされています。それを強制するために、変数の宣言セクションを暗黙の型付けをオフにするステートメントで開始します。ステートメントは次のとおりです。

implicit none

詳細については、implicit ステートメントを参照してください。

aimag#

名前#

aimag(3) - [TYPE:NUMERIC] 複素数の虚数部

概要#

    result = aimag(z)
     elemental complex(kind=KIND) function aimag(z)

      complex(kind=KIND),intent(in) :: z

特性#

  • 引数 z の型は complex であり、サポートされている任意の complex の種類である必要があります。

  • 戻り値は、引数の種類型パラメーターを持つ real 型です。

説明#

aimag(3) は、複素引数 z の虚数部を返します。

これは、値の虚数部を指定する最新の複素数部分指定子 %IM に似ています。ただし、指定子は、val%im=10.0 のように、代入の左辺にも表示できます。

オプション#

  • z

    虚数部を抽出する complex 値。

結果#

戻り値は、引数 z の虚数部の大きさと符号を持つ real 値です。

つまり、z の値が (x,y) の場合、結果の値は y です。

#

サンプルプログラム

program demo_aimag
use, intrinsic :: iso_fortran_env, only : real_kinds, &
 & real32, real64, real128
implicit none
character(len=*),parameter :: g='(*(1x,g0))'
complex              :: z4
complex(kind=real64) :: z8
   ! basics
    z4 = cmplx(1.e0, 2.e0)
    print *, 'value=',z4
    print g, 'imaginary part=',aimag(z4),'or', z4%im

    ! other kinds other than the default may be supported
    z8 = cmplx(3.e0_real64, 4.e0_real64,kind=real64)
    print *, 'value=',z8
    print g, 'imaginary part=',aimag(z8),'or', z8%im

    ! an elemental function can be passed an array
    print *
    print *, [z4,z4/2.0,z4+z4,z4**3]
    print *
    print *, aimag([z4,z4/2.0,z4+z4,z4**3])

end program demo_aimag

結果

 value= (1.00000000,2.00000000)
 imaginary part= 2.00000000 or 2.00000000
 value= (3.0000000000000000,4.0000000000000000)
 imaginary part= 4.0000000000000000 or 4.0000000000000000

 (1.00000000,2.00000000) (0.500000000,1.00000000) (2.00000000,4.00000000)
 (-11.0000000,-2.00000000)

   2.00000000       1.00000000       4.00000000      -2.00000000

標準#

FORTRAN 77

参照#

Fortran は、代数式や論理式に加えて、複素数値を返すまたは受け取る多くの組み込み関数など、複素数値を強力にサポートしています。

abs(3)acosh(3)acos(3)asinh(3)asin(3)atan2(3)atanh(3)atan(3)cosh(3)cos(3)co_sum(3)dble(3)dot_product(3)exp(3)int(3)is_contiguous(3)kind(3)log(3)matmul(3)precision(3)product(3)range(3)rank(3)sinh(3)sin(3)sqrt(3)storage_size(3)sum(3)tanh(3)tan(3)unpack(3)

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

cmplx#

名前#

cmplx(3) - [TYPE:NUMERIC] 複素数型への変換

概要#

    result = cmplx(x [,kind]) | cmplx(x [,y] [,kind])
     elemental complex(kind=KIND) function cmplx( x, y, kind )

      type(TYPE(kind=**)),intent(in)          :: x
      type(TYPE(kind=**)),intent(in),optional :: y
      integer(kind=**),intent(in),optional    :: KIND

特性#

  • x は、整数実数、または 複素数 にすることができます。

  • y は、整数 または 実数 にすることができます。y は、x複素数 でない場合にのみ許可されます。

  • KIND は、結果の種類パラメーターを示す定数 整数 初期化式です。

引数の型は、複素数x 値を除き、結果の種類に影響しません。

  • kind が存在せず、x複素数 の場合、結果は x の種類になります。

  • kind が存在せず、x複素数 でない場合、結果はデフォルトの 複素数 の種類になります。

注: ** として指定された種類は、その型でサポートされている任意の種類の可能性があります。

説明#

cmplx(3) 関数は、数値値を 複素数 値に変換します。

定数は、次のような構文を使用して複素数変数を定義するために使用できますが、

      z = (1.23456789, 9.87654321)

これは変数では機能しません。したがって、次のように入力することはできません。

      z = (a, b)  ! NO ! (unless a and b are constants, not variables)

したがって、非複素数を使用して 複素数 値を構築するには、cmplx(3) 関数を使用する必要があります。

      z = cmplx(a, b)

または、%IM および %RE 指定子を使用して、虚数部と実数部に値を個別に割り当てます。

      z%re = a
      z%im = b

x が複素数の場合、y は許可されず、cmplx は基本的に入力値を返しますが、種類を任意に変更できます。これは、引数が異なる種類を持つ必要がある(値を変更せずに返す)プロシージャに値を渡す場合に役立ちます。

      call something(cmplx(z,kind=real64))

z の種類が異なる場合でも、kind=real64 の値のコピーを渡します。

それ以外の場合は、単純な代入と同等です。したがって、z1 と z2 が 複素数 の場合、

      z2 = z1        ! equivalent statements
      z2 = cmplx(z1)

x複素数 でない場合、x は結果の実数部を定義するためにのみ使用されますが、y は仍然としてオプションです。結果の虚数部には値 0 が割り当てられるだけです。

y が存在する場合、虚数部に変換されます。

cmplx(3) と倍精度#

主に上位互換性を維持するために、デフォルトよりも高い精度の複素数を扱う場合は注意が必要です。

FORTRAN 77 で規定された動作であるため、cmplx(3) は kind オプションがない場合、常にデフォルト kind の結果を返すように指定し続ける必要がありました。

戻り値の kind を決定するために引数の最高精度を使用するのが望ましいかもしれませんが、そうではありません。したがって、デフォルト値よりも高い精度の引数を使用する場合は、kind 引数を使用するか、より高い精度の値がデフォルト精度に縮小される必要があります。

これは、d1d2倍精度 の場合、cmplx(d1,d2) は次のように扱われることを意味します。

      cmplx(sngl(d1), sngl(d2))

これにより精度が失われます。

そのため、Fortran 90 は、複素数の結果の希望する kind を指定するために使用される追加の引数を追加することにより、cmplx(3) 組み込み関数を拡張しました。

      integer,parameter :: dp=kind(0.0d0)
      complex(kind=dp) :: z8
     ! wrong ways to specify constant values
      ! note this was stored with default real precision !
      z8 = cmplx(1.2345678901234567d0, 1.2345678901234567d0)
      print *, 'NO, Z8=',z8,real(z8),aimag(z8)

      z8 = cmplx(1.2345678901234567e0_dp, 1.2345678901234567e0_dp)
      ! again, note output components are just real
      print *, 'NO, Z8=',z8,real(z8),aimag(z8)
      !
      ! YES
      !
      ! kind= makes it work
      z8 = cmplx(1.2345678901234567d0, 1.2345678901234567d0,kind=dp)
      print *, 'YES, Z8=',z8,real(z8),aimag(z8)

cmplx(3) を使用する最近の代替手段は、「F2018 コンポーネント構文」であり、複素エンティティの実部と虚部に個別にアクセスできます。

value%RE     ! %RE specifies the real part
or
value%IM     ! %IM specifies the imaginary part

もちろん、指定子の値は複素数型です。

複素数部分指定子の型は 実数 であり、その kind と形状は指定子の型と形状と同じです。つまり、cmplx とは異なり、デフォルトで複素数の精度が保持されます。

以下は複素数部分指定子の例です。

       impedance%re           !-- Same value as real(impedance)
       fft%im                 !-- Same value as AIMAG(fft)
       x%im = 0.0             !-- Sets the imaginary part of x to zero
       x(1:2)%re=[10,20]      !-- even if x is an array

I/O に関する注記#

書式指定ステートメントが指定されている場合、複素数値は 2 つの実数値として扱われることに注意してください。

リスト指定 I/O (つまり、書式にアスタリスクを使用) および NAMELIST 出力の場合、値は「(」と「)」で区切られ、「(実数部,虚数部)」の形式であることが期待されます。 NAMELIST 入力の場合、括弧で囲まれた値または複数の _実数_ 値のリストが受け入れられます。

オプション#

  • x

    x が複素数ではない場合に、結果の 実数 コンポーネントに割り当てられる値。

    x が複素数の場合、結果は、入力の実数部が x として、虚数部が y として渡された場合と同じになります。

     result = CMPLX (REAL (X), AIMAG (X), KIND).

つまり、複素数の x 値は、kind が変更される可能性はありますが、結果値にコピーされます。

  • y

    y は、x複素数 でない場合にのみ許可されます。その値は結果の虚数コンポーネントに割り当てられ、ない場合はゼロの値がデフォルトになります。

  • kind

    結果の kind パラメータを示す 整数 初期化式。

結果#

戻り値は 複素数 型で、大きさは xy の値によって決定されます。

x が複素数でない一般的なケースでは、結果の実数コンポーネントには x の値が割り当てられ、虚数部はゼロか、y が存在する場合は y の値が割り当てられます。

x が複素数の場合、y は許可されず、結果は kind が変更される可能性があることを除き、x と同じ値になります。つまり、実数部は real(x, kind) であり、虚数部は real(y, kind) です。

#

サンプルプログラム

program demo_aimag
implicit none
integer,parameter :: dp=kind(0.0d0)
real(kind=dp)     :: precise
complex(kind=dp)  :: z8
complex           :: z4, zthree(3)
   precise=1.2345678901234567d0

  ! basic
   z4 = cmplx(-3)
   print *, 'Z4=',z4
   z4 = cmplx(1.23456789, 1.23456789)
   print *, 'Z4=',z4
   ! with a format treat a complex as two real values
   print '(1x,g0,1x,g0,1x,g0)','Z4=',z4

  ! working with higher precision values
   ! using kind=dp makes it keep DOUBLEPRECISION precision
   ! otherwise the result would be of default kind
   z8 = cmplx(precise, -precise )
   print *, 'lost precision Z8=',z8
   z8 = cmplx(precise, -precise ,kind=dp)
   print *, 'kept precision Z8=',z8

  ! assignment of constant values does not require cmplx(3)00
   ! The following is intuitive and works without calling cmplx(3)
   ! but does not work for variables just constants
   z8 = (1.1111111111111111d0, 2.2222222222222222d0 )
   print *, 'Z8 defined with constants=',z8

  ! what happens when you assign a complex to a real?
   precise=z8
   print *, 'LHS=',precise,'RHS=',z8

  ! elemental
   zthree=cmplx([10,20,30],-1)
   print *, 'zthree=',zthree

  ! descriptors are an alternative
   zthree(1:2)%re=[100,200]
   print *, 'zthree=',zthree

end program demo_aimag

結果

    Z4= (-3.000000,0.0000000E+00)
    Z4= (1.234568,1.234568)
    Z4= 1.234568 1.234568
    lost precision Z8= (1.23456788063049,-1.23456788063049)
    kept precision Z8= (1.23456789012346,-1.23456789012346)
    Z8 defined with constants= (1.11111111111111,2.22222222222222)
    LHS=   1.11111111111111      RHS= (1.11111111111111,2.22222222222222)
    zthree= (10.00000,-1.000000) (20.00000,-1.000000) (30.00000,-1.000000)
    zthree= (100.0000,-1.000000) (200.0000,-1.000000) (30.00000,-1.000000)

標準#

FORTRAN 77、KIND は Fortran 90 で追加されました。

関連項目#

Fortran は、代数式や論理式に加えて、複素数値を返すまたは受け取る多くの組み込み関数など、複素数値を強力にサポートしています。

abs(3)acosh(3)acos(3)asinh(3)asin(3)atan2(3)atanh(3)atan(3)cosh(3)cos(3)co_sum(3)dble(3)dot_product(3)exp(3)int(3)is_contiguous(3)kind(3)log(3)matmul(3)precision(3)product(3)range(3)rank(3)sinh(3)sin(3)sqrt(3)storage_size(3)sum(3)tanh(3)tan(3)unpack(3)

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

int#

名前#

int(3) - [TYPE:NUMERIC] ゼロに向かって切り捨てて整数に変換

概要#

    result = int(a [,kind])
     elemental integer(kind=KIND) function int(a, KIND )

      TYPE(kind=**),intent(in) :: a
      integer,optional :: KIND

特性#

  • ** で指定された kind は、その型のサポートされている任意の kind にすることができます

  • a は、整数、実数、または複素数の型、または boz-literal-constant である必要があります。

  • KIND は、スカラー整数定数式である必要があります。

説明#

int(3) はゼロに向かって切り捨てて、整数 を返します。

オプション#

  • a

    ゼロに向かって切り捨てる値です

  • kind

    結果の kind パラメータを示します。存在しない場合、返される型はデフォルトの整数型です。

結果#

次の規則を適用して 整数 変数を返します

ケース:

  1. a整数 型の場合、int(a) = a

  2. a実数 型で |a| < 1 の場合、int(a)0 に等しくなります。|a| >= 1 の場合、int(a) は、大きさが a を超えず、符号が a の符号と同じである整数に等しくなります。

  3. a複素数 型の場合、規則 2 が a実数 部分に適用されます。

  4. a が boz-literal 定数の場合、指定された kind整数 として扱われます。

    最上位ビットが 1 であるビットシーケンスの解釈は、プロセッサに依存します。

指定された整数型で表現できない場合、結果は未定義です。

#

サンプルプログラム

program demo_int
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer :: i = 42
complex :: z = (-3.7, 1.0)
real :: x=-10.5, y=10.5

   print *, int(x), int(y)

   print *, int(i)

   print *, int(z), int(z,8)
   ! elemental
   print *, int([-10.9,-10.5,-10.3,10.3,10.5,10.9])
   ! note int(3) truncates towards zero

   ! CAUTION:
   ! a number bigger than a default integer can represent
   ! produces an incorrect result and is not required to
   ! be detected by the program.
   x=real(huge(0))+1000.0
   print *, int(x),x
   ! using a larger kind
   print *, int(x,kind=int64),x

   print *, int(&
   & B"111111111111111111111111111111111111111111111111111111111111111",&
   & kind=int64)
   print *, int(O"777777777777777777777",kind=int64)
   print *, int(Z"7FFFFFFFFFFFFFFF",kind=int64)

   ! elemental
   print *
   print *,int([ &
   &  -2.7,  -2.5, -2.2, -2.0, -1.5, -1.0, -0.5, &
   &  0.0,   &
   &  +0.5,  +1.0, +1.5, +2.0, +2.2, +2.5, +2.7  ])

end program demo_int

結果

 >          -10   10
 >           42
 >           -3  -3
 >          -10  -10  -10   10   10  10
 >  -2147483648   2.14748467E+09
 >   2147484672   2.14748467E+09
 >   9223372036854775807
 >   9223372036854775807
 >   9223372036854775807
 >
 >  -2          -2          -2          -2          -1
 >  -1           0           0           0           1
 >   1           2           2           2           2

標準#

FORTRAN 77

関連項目#

aint(3)anint(3)nint(3)selected_int_kind(3)ceiling(3)floor(3)

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

nint#

名前#

nint(3) - [TYPE:NUMERIC] 最も近い整数

概要#

    result = nint( a [,kind] )
     elemental integer(kind=KIND) function nint(a, kind )

      real(kind=**),intent(in) :: a
      integer(kind=**),intent(in),optional :: KIND

特性#

  • ** で指定された kind は、その型のサポートされている任意の kind にすることができます

  • a は任意の kind の実数型です

  • KIND は、スカラー整数定数式です

  • 結果は、デフォルトの 整数 kind または、kind が存在する場合は kind の値になります。

説明#

nint(3) は、引数を符号を保持したまま、最も近い整数に丸めます。

ユーザーは、値が返される kind の範囲に対して有効な値であることを確認する必要があります。プロセッサが指定された kind で結果を表現できない場合、結果は未定義です。

a がゼロより大きい場合、nint(a) は値 int(a+0.5) を持ちます。

a がゼロ以下の場合、nint(a) は値 int(a-0.5) を持ちます。

オプション#

  • a

    最も近い整数に丸める値

  • kind

    出力値の kind を指定できます。存在しない場合、出力はデフォルトの 整数 型になります。

結果#

結果は a に最も近い整数です。または、a に等しく近い整数が 2 つある場合、結果は、そのような 整数 のうち、より大きな大きさを持つ方になります。

指定された整数型で表現できない場合、結果は未定義です。

#

サンプルプログラム

program demo_nint
implicit none
integer,parameter   :: dp=kind(0.0d0)
real,allocatable    :: in(:)
integer,allocatable :: out(:)
integer             :: i
real                :: x4
real(kind=dp)       :: x8

  ! basic use
   x4 = 1.234E0
   x8 = 4.721_dp
   print *, nint(x4), nint(-x4)
   print *, nint(x8), nint(-x8)

  ! elemental
   in = [ -2.7,  -2.5, -2.2, -2.0, -1.5, -1.0, -0.5, -0.4, &
        &  0.0,   &
        & +0.04, +0.5, +1.0, +1.5, +2.0, +2.2, +2.5, +2.7  ]
   out = nint(in)
   do i=1,size(in)
      write(*,*)in(i),out(i)
   enddo

  ! dusty corners
   ISSUES: block
   use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
   integer :: icheck
      ! make sure input is in range for the type returned
      write(*,*)'Range limits for typical KINDS:'
      write(*,'(1x,g0,1x,g0)')  &
      & int8,huge(0_int8),   &
      & int16,huge(0_int16), &
      & int32,huge(0_int32), &
      & int64,huge(0_int64)

      ! the standard does not require this to be an error ...
      x8=12345.67e15 ! too big of a number
      icheck=selected_int_kind(ceiling(log10(x8)))
      write(*,*)'Any KIND big enough? ICHECK=',icheck
      print *, 'These are all wrong answers for ',x8
      print *, nint(x8,kind=int8)
      print *, nint(x8,kind=int16)
      print *, nint(x8,kind=int32)
      print *, nint(x8,kind=int64)
   endblock ISSUES

end program demo_nint

結果

 >               1          -1
 >               5          -5
 >      -2.700000              -3
 >      -2.500000              -3
 >      -2.200000              -2
 >      -2.000000              -2
 >      -1.500000              -2
 >      -1.000000              -1
 >     -0.5000000              -1
 >     -0.4000000               0
 >      0.0000000E+00           0
 >      3.9999999E-02           0
 >      0.5000000               1
 >       1.000000               1
 >       1.500000               2
 >       2.000000               2
 >       2.200000               2
 >       2.500000               3
 >       2.700000               3
 >     Range limits for typical KINDS:
 >     1 127
 >     2 32767
 >     4 2147483647
 >     8 9223372036854775807
 >     Any KIND big enough? ICHECK=          -1
 >     These are all wrong answers for   1.234566949990144E+019
 >        0
 >          0
 >     -2147483648
 >      -9223372036854775808

標準#

FORTRAN 77、KIND 引数 - Fortran 90

関連項目#

aint(3)anint(3)int(3)selected_int_kind(3)ceiling(3)floor(3)

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

real#

名前#

real(3) - [TYPE:NUMERIC] 実数型に変換

概要#

  result = real(x [,kind])
   elemental real(kind=KIND) function real(x,KIND)

    TYPE(kind=**),intent(in) :: x
    integer(kind=**),intent(in),optional :: KIND

特性#

  • x の型は、整数実数、または 複素数、または BOZ-literal-constant である可能性があります。

  • kind整数 初期化式 (定数式) です

    • kind が存在する場合、実数 の結果の kind を定義します

    • kind が存在しない場合

      • x複素数 の場合、結果は x と同じ kind の 実数 になります。

      • x実数 または 整数 の場合、結果はデフォルト kind の 実数 になります

  • ** で指定された kind は、その型のサポートされている任意の kind にすることができます

説明#

real(3) は、引数 x実数 型に変換します。

複素数値の実数部が返されます。複素数値の場合、これは、複素数 値の実数部も指定する最新の複素数部分指定子 %RE と似ています。

      z=(3.0,4.0)     ! if z is a complex value
      print *, z%re == real(z) ! these expressions are equivalent

オプション#

  • x

    実数 に変換する 整数実数、または 複素数 の値。

  • kind

    存在する場合、kind の値は結果の kind を定義します。

結果#

  1. real(x) は、x整数 または 実数 変数の場合、x をデフォルトの 実数 型に変換します。

  2. real(x) は、複素数値を、入力 x と同じ種別型パラメーターを持つ、実数部の大きさを持つ実数型に変換します。

  3. real(x, kind) は、x複素数整数、または実数変数の場合、種別型パラメーター kind を持つ実数型に変換されます。

#

サンプルプログラム

program demo_real
use,intrinsic :: iso_fortran_env, only : dp=>real64
implicit none
complex              :: zr = (1.0, 2.0)
doubleprecision      :: xd=huge(3.0d0)
complex(kind=dp) :: zd=cmplx(4.0e0_dp,5.0e0_dp,kind=dp)

   print *, real(zr), aimag(zr)
   print *, dble(zd), aimag(zd)

   write(*,*)xd,real(xd,kind=kind(0.0d0)),dble(xd)
end program demo_real

結果

 1.00000000       2.00000000
 4.0000000000000000       5.0000000000000000
 1.7976931348623157E+308  1.7976931348623157E+308  1.7976931348623157E+308

標準#

FORTRAN 77

参照#

Fortran は、代数式や論理式に加えて、複素数値を返すまたは受け取る多くの組み込み関数など、複素数値を強力にサポートしています。

abs(3)acosh(3)acos(3)asinh(3)asin(3)atan2(3)atanh(3)atan(3)cosh(3)cos(3)co_sum(3)dble(3)dot_product(3)exp(3)int(3)is_contiguous(3)kind(3)log(3)matmul(3)precision(3)product(3)range(3)rank(3)sinh(3)sin(3)sqrt(3)storage_size(3)sum(3)tanh(3)tan(3)unpack(3)

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

dble#

名前#

dble(3) - [種類:数値] 倍精度実数への変換

概要#

    result = dble(a)
     elemental doubleprecision function dble(a)

      doubleprecision :: dble
      TYPE(kind=KIND),intent(in) :: a

特性#

  • a は、整数実数複素数、または BOZ リテラル定数である可能性があります。

  • 結果は倍精度実数です。

説明#

dble(3) は、a を倍精度実数型に変換します。

オプション#

  • a

    倍精度実数に変換する値。

結果#

戻り値は倍精度型です。複素数入力の場合、返される値は入力値の実数部の大きさと符号を持ちます。

#

サンプルプログラム

program demo_dble
implicit none
real:: x = 2.18
integer :: i = 5
complex :: z = (2.3,1.14)
   print *, dble(x), dble(i), dble(z)
end program demo_dble

結果

  2.1800000667572021  5.0000000000000000   2.2999999523162842

標準#

FORTRAN 77

参照#

  • aimag(3) - 複素数の虚数部

  • cmplx(3) - 複素数型への値の変換

  • int(3) - ゼロに向かって切り捨てて整数に変換

  • nint(3) - 最も近い整数

  • out_of_range(3) - 値が安全に変換できないかどうか。

  • real(3) - 実数型への変換

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

transfer#

名前#

transfer(3) - [種類:モールド] ビットパターンの転送

概要#

    result = transfer(source, mold [,size] )
     type(TYPE(kind=KIND)) function transfer(source,mold,size)

      type(TYPE(kind=KIND)),intent(in) :: source(..)
      type(TYPE(kind=KIND)),intent(in) :: mold(..)
      integer(kind=**),intent(in),optional :: size

特性#

  • source は、任意のスカラーまたは配列です。

  • mold は、任意のスカラーまたは配列です。

  • size は、整数型のスカラーです。

  • result は、mold と同じ型を持ちます。

説明#

transfer(3) は、メモリ内の source のビット単位の表現を、mold と同じ型および型パラメーターの変数または配列にコピーします。

これは、C の「型キャスト」の概念とほぼ同等です。

オプション#

  • source

    コピーするビットパターンを保持します。

  • mold

    mold の型は、戻り値の型を定義するために使用されます。さらに、配列の場合、戻り値は 1 次元配列です。スカラーの場合、戻り値はスカラーです。

  • size

    size が存在する場合、結果は長さ size の 1 次元配列になります。

size が存在しないが、mold が(任意サイズまたは形状の)配列の場合、結果は source のビット単位の表現全体を含めるために必要な最小長の 1 次元配列になります。

size が存在せず、mold がスカラーの場合、結果はスカラーになります。

結果#

結果は、source のビットレベルの表現を持ちます。

結果のビット単位の表現が source のものよりも長い場合、結果の先頭ビットは source のものに対応しますが、末尾ビットは任意に埋められます。

結果のビット表現が mold と同じ型の変数の有効な表現に対応しない場合、結果は未定義であり、結果に対する後続の操作は、意味のある動作を生成することを保証できません。たとえば、var と .not. var の両方が true であるように見える論理変数を作成することが可能です。

#

サンプルプログラム

program demo_transfer
use,intrinsic :: iso_fortran_env, only : int32, real32
integer(kind=int32) :: i = 2143289344
real(kind=real32)   :: x
character(len=10)   :: string
character(len=1)    :: chars(10)
   x=transfer(i, 1.0)    ! prints "nan" on i686
   ! the bit patterns are the same
   write(*,'(b0,1x,g0)')x,x ! create a NaN
   write(*,'(b0,1x,g0)')i,i

   ! a string to an array of characters
   string='abcdefghij'
   chars=transfer(string,chars)
   write(*,'(*("[",a,"]":,1x))')string
   write(*,'(*("[",a,"]":,1x))')chars
end program demo_transfer

結果

   1111111110000000000000000000000 NaN
   1111111110000000000000000000000 2143289344
   [abcdefghij]
   [a] [b] [c] [d] [e] [f] [g] [h] [i] [j]

コメント#

Joe Krahn: Fortran は、キャスティングではなく、モールドを使用します。

C のように、キャスティングはインプレースの再解釈です。キャストは、オブジェクトの形状を変更するためにオブジェクトの周囲に構築されたデバイスです。

Fortran transfer(3) は、データをアウトオブプレースで再解釈します。これは、キャスティングではなくモールドと考えることができます。モールドは、その中に入れられたオブジェクトに形状を与えるデバイスです。

モールドの利点は、データが常にそれを保持する変数のコンテキストで有効であることです。多くの場合、適切なコンパイラーは transfer(3) を単純な代入に最適化する必要があります。

このアプローチには欠点があります。最大データオブジェクトを知る必要があるため、データ型のユニオンを定義するのは問題があります。これは、コンパイラーまたはコンパイルオプションによって異なる可能性があります。多くの場合、EQUIVALENCE ははるかに効果的ですが、Fortran Standards 委員会は、EQUIVALENCE を控えめに使用した場合の利点に気づいていないようです。

標準#

Fortran 90

参照#

****(3)

fortran-lang の組み込み関数の説明

logical#

名前#

logical(3) - [種類:論理] 論理値の種類の間の変換

概要#

    result = logical(l [,kind])
     elemental logical(kind=KIND) function logical(l,KIND)

      logical(kind=**),intent(in) :: l
      integer(kind=**),intent(in),optional :: KIND

特性#

  • ** で指定された kind は、その型のサポートされている任意の kind にすることができます

  • l は論理型です。

  • KIND は、スカラー整数定数式です。KIND が存在する場合、結果の種別型パラメーターは、KIND の値によって指定されたものです。それ以外の場合、種別型パラメーターはデフォルトの論理型です。

説明#

logical(3) は、ある種類の論理変数を別の種類に変換します。

オプション#

  • l

    種類 kind のコピーを作成する論理

  • kind

    結果の種類のパラメーターを示します。存在しない場合は、デフォルトの種類が返されます。

結果#

戻り値は、l と等しい論理値であり、kind に対応する種類、または kind が指定されていない場合はデフォルトの論理型です。

#

サンプルプログラム

Linux
program demo_logical
! Access array containing the kind type parameter values supported by this
! compiler for entities of logical type
use iso_fortran_env, only : logical_kinds
implicit none
integer :: i

   ! list kind values supported on this platform, which generally vary
   ! in storage size as alias declarations
   do i =1, size(logical_kinds)
      write(*,'(*(g0))')'integer,parameter :: boolean', &
      & logical_kinds(i),'=', logical_kinds(i)
   enddo

end program demo_logical

結果

 > integer,parameter :: boolean1=1
 > integer,parameter :: boolean2=2
 > integer,parameter :: boolean4=4
 > integer,parameter :: boolean8=8
 > integer,parameter :: boolean16=16

標準#

Fortran 95、関連する ISO_FORTRAN_ENV モジュール - fortran 2009

参照#

int(3), real(3), cmplx(3)

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

kind#

名前#

kind(3) - [種類:照会] エンティティの種類の照会

概要#

    result = kind(x)
     integer function kind(x)

      type(TYPE,kind=**),intent(in) :: x(..)

特性#

  • x は、任意の組み込み型にすることができます。スカラーまたは配列のいずれかです。

  • 結果はデフォルトの整数スカラーです。

説明#

kind(x)(3) は、エンティティ x の種類の値を返します。

オプション#

  • x

    種類を照会する値。

結果#

戻り値は、引数 x の種類を示します。

種類はプロセッサに依存することに注意してください。

#

サンプルプログラム

program demo_kind
implicit none
integer,parameter :: dc = kind(' ')
integer,parameter :: dl = kind(.true.)

   print *, "The default character kind is ", dc
   print *, "The default logical kind is ", dl

end program demo_kind

結果

    The default character kind is            1
    The default logical kind is            4

標準#

Fortran 95

参照#

  • allocated(3) - 割り付け可能なエンティティの状態

  • is_contiguous(3) - オブジェクトが連続しているかどうかをテストします

  • lbound(3) - 配列の下限境界

  • rank(3) - データオブジェクトのランク

  • shape(3) - 配列の形状を決定します

  • size(3) - 配列のサイズを決定します

  • ubound(3) - 配列の上限境界

  • bit_size(3) - ビットサイズの照会関数

  • storage_size(3) - ビット単位のストレージサイズ

  • kind(3) - エンティティの種類

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

out_of_range#

名前#

out_of_range(3) - [種類:数値] 値が安全に変換できないかどうか。

概要#

    result = out_of_range (x, mold [, round])
     elemental logical function(x, mold, round)

      TYPE,kind=KIND),intent(in) :: x
      TYPE,kind=KIND),intent(in) :: mold
      logical,intent(in),optional     :: round

特性#

  • x は、整数型または実数型です。

  • mold は、整数または実数スカラーです。

  • round は、論理スカラーです。

  • 結果はデフォルトの論理型です。

説明#

out_of_range(3) は、値 xmold と同じ型および種類を持つ実数または整数変数に安全に変換できるかどうかを判断します。

たとえば、int8 が 8 ビットの 2 進整数型の種類の値である場合、out_of_range(-128.5, 0_int8) の値は false になり、out_of_range(-128.5, 0_int8, .true.) の値は .true. になります。これは、整数に変換するときに値が切り捨てられ、-128 が 8 ビットの 2 の補数マシンで表現可能な値であっても、+128 はそうではないためです。

オプション#

  • x

    mold の型と種類を持つ変数に格納できるかどうかをテストするスカラー

  • mold と kind は、適合させる必要のあるものの特性を決定するためにクエリされます。

  • 丸め

    xx の値を、mold のような整数値として検証する前に丸めるかどうかを示すフラグ。

    round は、x が *real* 型で、mold が *integer* 型の場合にのみ存在できます。

結果#

標準から

ケース (i): mold が整数型で、round が存在しないか、false の値で存在する場合、結果は、X の値が IEEE 無限大または NaN である場合、または 0 と X の間にある最大の絶対値を持つ整数が、mold の型と kind を持つオブジェクトで表現できない場合にのみ true になります。

ケース (ii): mold が整数型で、round が true の値で存在する場合、結果は、X の値が IEEE 無限大または NaN である場合、または X に最も近い整数、または X に等距離にある 2 つの整数がある場合はより大きい絶対値を持つ整数が、mold の型と kind を持つオブジェクトで表現できない場合にのみ true になります。

ケース (iii): それ以外の場合、結果は、X の値が mold の型と kind のオブジェクトでサポートされていない IEEE 無限大または NaN である場合、または X が有限数であり、X の値を (必要に応じて IEEE 丸めモードに従って) mold の kind の拡張モデルに丸めた結果が、mold の型と kind を持つオブジェクトで表現可能な、X と同じ符号を持つ最大の有限数の絶対値よりも大きい場合にのみ true になります。

mold は、そこから取得される情報が型と kind のみであるため、スカラーである必要があります。配列 mold を許可すると、x と適合可能である必要が生じます。配列の丸めモードを許可すると、多くのプロセッサでパフォーマンスが大幅に低下するため、round はスカラーです。

#

サンプルプログラム

program demo_out_of_range
use, intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
use, intrinsic :: iso_fortran_env, only : real32, real64, real128
implicit none
integer            :: i
integer(kind=int8) :: i8, j8

    ! compilers are not required to produce an error on out of range.
    ! here storing the default integers into 1-byte integers
    ! incorrectly can have unexpected results
    do i=127,130
       i8=i
       j8=-i
       ! OUT_OF_RANGE(3f) can let you check if the value will fit
       write(*,*)i8,j8,' might have expected',i,-i, &
        & out_of_range( i,i8), &
        & out_of_range(-i,i8)
    enddo
    write(*,*) 'RANGE IS ',-1-huge(0_int8),'TO',huge(0_int8)
    ! the real -128.5 is truncated to -128 and is in range
    write(*,*) out_of_range (  -128.5, 0_int8)         ! false

    ! the real -128.5 is rounded to -129 and is not in range
    write(*,*) out_of_range (  -128.5, 0_int8, .true.) ! true

end program demo_out_of_range

結果

  >  127 -127  might have expected         127        -127 F F
  > -128 -128  might have expected         128        -128 T F
  > -127  127  might have expected         129        -129 T T
  > -126  126  might have expected         130        -130 T T
  > RANGE IS         -128 TO  127
  > F
  > T

標準#

FORTRAN 2018

参照#

  • aimag(3) - 複素数の虚数部

  • cmplx(3) - 複素数型への値の変換

  • dble(3) - 倍精度変換関数

  • int(3) - ゼロに向かって切り捨てて整数に変換

  • nint(3) - 最も近い整数

  • real(3) - 実数型への変換

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

selected_char_kind#

名前#

selected_char_kind(3) - [KIND] "Unicode"などの文字種を選択

概要#

    result = selected_char_kind(name)
     integer function selected_char_kind(name)

      character(len=*),intent(in) :: name

特性#

  • name はデフォルトの *character* スカラーです。

  • 結果はデフォルトの整数スカラーです。

説明#

selected_char_kind(3) は、name という名前の文字セットの kind パラメータ値を返します。

名前がサポートされていない場合は、-1 が返されます。それ以外の場合、結果はその kind 型パラメータ値に等しい値になります。

サポートされている名前のリストは、"DEFAULT" を除いてプロセッサに依存します。

  • name の値が "DEFAULT" の場合、結果はデフォルト文字の kind 型パラメータの値に等しい値になります。この名前は常にサポートされています。

  • name の値が "ASCII" の場合、結果は ASCII 文字の kind 型パラメータの値に等しい値になります。

  • name の値が "ISO_10646" の場合、結果は ISO 10646 文字種 (ISO/IEC 10646 で指定されている UCS-4 に対応) の kind 型パラメータの値に等しい値になります。

  • name がプロセッサでサポートされている他の文字種のプロセッサ定義の名前である場合、結果はその kind 型パラメータ値に等しい値になります。事前定義された名前には、"ASCII" および "ISO_10646" が含まれます。

NAME は、大文字と小文字、または末尾の空白に関係なく解釈されます。

オプション#

  • name

    プロセッサ依存の kind 値をクエリし、サポートされているかどうかを判断する名前。name は、大文字と小文字、または末尾の空白に関係なく解釈されます。

    現在、サポートされている文字セットには、"ASCII"、"DEFAULT"、および "ISO_10646" (ユニバーサル文字セット、UCS-4) が含まれており、これは一般に "Unicode" として知られています。"DEFAULT" 以外のサポートされている名前はプロセッサに依存します。

結果#

#

サンプルプログラム

Linux
program demo_selected_char_kind
use iso_fortran_env
implicit none

intrinsic date_and_time,selected_char_kind

! set some aliases for common character kinds
! as the numbers can vary from platform to platform

integer, parameter :: default = selected_char_kind ("default")
integer, parameter :: ascii =   selected_char_kind ("ascii")
integer, parameter :: ucs4  =   selected_char_kind ('ISO_10646')
integer, parameter :: utf8  =   selected_char_kind ('utf-8')

! assuming ASCII and UCS4 are supported (ie. not equal to -1)
! define some string variables
character(len=26, kind=ascii ) :: alphabet
character(len=30, kind=ucs4  ) :: hello_world
character(len=30, kind=ucs4  ) :: string

   write(*,*)'ASCII     ',&
    & merge('Supported    ','Not Supported',ascii /= -1)
   write(*,*)'ISO_10646 ',&
    & merge('Supported    ','Not Supported',ucs4 /= -1)
   write(*,*)'UTF-8     ',&
    & merge('Supported    ','Not Supported',utf8 /= -1)

   if(default.eq.ascii)then
       write(*,*)'ASCII is the default on this processor'
   endif

  ! for constants the kind precedes the value, somewhat like a
  ! BOZ constant
   alphabet = ascii_"abcdefghijklmnopqrstuvwxyz"
   write (*,*) alphabet

   hello_world = ucs4_'Hello World and Ni Hao -- ' &
                 // char (int (z'4F60'), ucs4)     &
                 // char (int (z'597D'), ucs4)

  ! an encoding option is required on OPEN for non-default I/O
   if(ucs4 /= -1 )then
      open (output_unit, encoding='UTF-8')
      write (*,*) trim (hello_world)
   else
      write (*,*) 'cannot use utf-8'
   endif

   call create_date_string(string)
   write (*,*) trim (string)

contains

! The following produces a Japanese date stamp.
subroutine create_date_string(string)
intrinsic date_and_time,selected_char_kind
integer,parameter :: ucs4 = selected_char_kind("ISO_10646")
character(len=1,kind=ucs4),parameter :: &
       nen =   char(int( z'5e74' ),ucs4), & ! year
       gatsu = char(int( z'6708' ),ucs4), & ! month
       nichi = char(int( z'65e5' ),ucs4)    ! day
character(len= *, kind= ucs4) string
integer values(8)
   call date_and_time(values=values)
   write(string,101) values(1),nen,values(2),gatsu,values(3),nichi
 101 format(*(i0,a))
end subroutine create_date_string

end program demo_selected_char_kind

結果

結果はプロセッサに大きく依存します。

 >  ASCII     Supported
 >  ISO_10646 Supported
 >  UTF-8     Not Supported
 >  ASCII is the default on this processor
 >  abcdefghijklmnopqrstuvwxyz
 >  Hello World and Ni Hao -- 你好
 >  2022年10月15日

標準#

Fortran 2003

参照#

selected_int_kind(3), selected_real_kind(3)

achar(3), char(3), ichar(3), iachar(3)

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

selected_int_kind#

名前#

selected_int_kind(3) - [KIND] 整数種を選択

概要#

    result = selected_int_kind(r)
    integer function selected_int_kind(r)

     integer(kind=KIND),intent(in) :: r

特性#

  • r は *integer* スカラーです。

  • 結果はデフォルトの整数スカラーです。

説明#

selected_int_kind(3) は、-10**r (排他的) から 10**r (排他的) までのすべての値を表現できる最小の整数型の kind 値を返します。この範囲に対応する整数種がない場合、selected_int_kind は -1 を返します。

オプション#

  • r

    値は、返される kind 型でサポートする必要がある 10 の累乗の必要な範囲を指定します。

結果#

結果は、要求された範囲内のすべての値を表現する整数型の kind 型パラメータの値に等しい値を持ちます。

プロセッサでそのような kind 型パラメータが利用できない場合、結果は -1 になります。

複数の kind 型パラメータが基準を満たす場合、返される値は、最小の 10 進数指数範囲を持つものになります。そのような値が複数ある場合は、これらの kind 値のうち最小のものが返されます。

#

サンプルプログラム

program demo_selected_int_kind
implicit none
integer,parameter :: k5 = selected_int_kind(5)
integer,parameter :: k15 = selected_int_kind(15)
integer(kind=k5) :: i5
integer(kind=k15) :: i15

    print *, huge(i5), huge(i15)

    ! the following inequalities are always true
    print *, huge(i5) >= 10_k5**5-1
    print *, huge(i15) >= 10_k15**15-1
end program demo_selected_int_kind

結果

  >   2147483647  9223372036854775807
  >  T
  >  T

標準#

Fortran 95

参照#

aint(3), anint(3), int(3), nint(3), ceiling(3), floor(3)

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost

selected_real_kind#

名前#

selected_real_kind(3) - [KIND] 実数種を選択

概要#

    result = selected_real_kind([p] [,r] [,radix] )
    integer function selected_int_kind(r)

     real(kind=KIND),intent(in),optional :: p
     real(kind=KIND),intent(in),optional :: r
     real(kind=KIND),intent(in),optional :: radix

特性#

  • p は *integer* スカラーです

  • r は *integer* スカラーです

  • radix は *integer* スカラーです

  • 結果はデフォルトの *integer* スカラーです

説明#

selected_real_kind(3) は、少なくとも p 桁の 10 進精度、少なくとも r の指数範囲、および radix の基数を持つ *real* データ型の kind 値を返します。つまり、そのような種類が存在する場合

+ it has the decimal precision as returned by **precision**(3) of at
  least **p** digits.
+ a decimal exponent range, as returned by the function **range**(3)
  of at least **r**
+ a radix, as returned by the function **radix**(3) , of **radix**,

要求された kind が存在しない場合は、-1 が返されます。

少なくとも 1 つの引数が存在する必要があります。

オプション#

  • p

    要求された精度

  • r

    要求された範囲

  • radix

    必要な基数

    Fortran 2008 より前は、引数 r または p のうち少なくとも 1 つが存在する必要がありました。Fortran 2008 以降では、存在しない場合はゼロと見なされます。

結果#

selected_real_kind は、少なくとも p 桁の 10 進精度、少なくとも R の 10 進数指数範囲、および要求された radix を持つ実数データ型の kind 型パラメータの値を返します。

p または r がない場合、結果の値はゼロの値で存在する場合と同じになります。

radix パラメータがない場合、選択した kind の基数に関する要件はなく、任意の基数を持つ実数種を返すことができます。

複数の実数データ型が基準を満たす場合、最小の 10 進精度を持つデータ型の kind が返されます。実数データ型が基準に一致しない場合、結果は

  • -1

    プロセッサが p 以上精度を持つ実数データ型をサポートしていないが、r および radix 要件を満たすことができる場合

  • -2

    プロセッサが r 以上指数範囲を持つ実数型をサポートしていないが、p および radix が満たせる場合

  • -3

    radix は満たせるが、p および r の要件は満たせない場合

  • -4

    radixp または r のいずれかの要件が満たせる場合

  • -5

    指定された radix の実数型がない場合

#

サンプルプログラム

program demo_selected_real_kind
implicit none
integer,parameter :: p6 = selected_real_kind(6)
integer,parameter :: p10r100 = selected_real_kind(10,100)
integer,parameter :: r400 = selected_real_kind(r=400)
real(kind=p6) :: x
real(kind=p10r100) :: y
real(kind=r400) :: z

   print *, precision(x), range(x)
   print *, precision(y), range(y)
   print *, precision(z), range(z)
end program demo_selected_real_kind

結果

  >            6          37
  >           15         307
  >           18        4931

標準#

Fortran 95; RADIX 付き - Fortran 2008

参照#

precision(3), range(3), radix(3)

fortran-lang 組み込み関数の説明 (ライセンス: MIT) @urbanjost