Fortran-quick-guide
Fortran-概要
式変換システムから派生したFortranは、汎用の命令型プログラミング言語です。 数値計算および科学計算に使用されます。
Fortranは、科学および工学アプリケーション向けに1950年代にIBMによって最初に開発されました。 Fortranはこのプログラミング領域を長い間支配しており、高性能コンピューティングで非常に人気がありました。
それはサポートしています-
- 数値解析と科学計算
- 構造化プログラミング
- 配列プログラミング
- モジュール式プログラミング
- 汎用プログラミング
- スーパーコンピューターでの高性能コンピューティング
- オブジェクト指向プログラミング
- 並行プログラミング
- コンピュータシステム間の合理的な移植性
Fortranについての事実
- Fortranは、1957年にIBMのジョンバックスが率いるチームによって作成されました。
- 当初は名前はすべて大文字で表記されていましたが、現在の標準と実装では最初の文字が大文字である必要があります。
- FortranはFORmula TRANslatorの略です。
- 元々は科学計算用に開発されたもので、汎用プログラミングに必要な文字列やその他の構造のサポートは非常に限られていた。
- その後の拡張と開発により、移植性の高い高レベルのプログラミング言語になりました。
- 現在、Fortran I、II、およびIIIのオリジナルバージョンは廃止されています。
- まだ使用されている最も古いバージョンは、Fortran IVおよびFortran 66です。
- 現在最も一般的に使用されているバージョンは、Fortran 77、Fortran 90、およびFortran 95です。
- Fortran 77は、文字列を特殊タイプとして追加しました。
- Fortran 90は、さまざまな種類のスレッド化と配列の直接処理を追加しました。
Fortran-環境設定
WindowsでのFortranのセットアップ
G95はGNU Fortranマルチアーキテクチャコンパイラで、WindowsでFortranをセットアップするために使用されます。 Windowsバージョンは、WindowsでMingWを使用してUNIX環境をエミュレートします。 インストーラーがこれを処理し、自動的にg95をWindowsのPATH変数に追加します。
G95の安定版はhttps://www.fortran.com/the-fortran-company-homepage/whats-new/g95-windows-download/[こちら]から入手できます。
G95の使用方法
インストール中に、オプション「推奨」を選択すると、 g95 がPATH変数に自動的に追加されます。 これは、新しいコマンドプロンプトウィンドウを開いて「g95」と入力するだけでコンパイラを起動できることを意味します。 以下の基本的なコマンドを見つけて、開始してください。
Sr.No | Command & Description |
---|---|
1 |
g95 –c hello.f90 hello.f90をhello.oという名前のオブジェクトファイルにコンパイルします。 |
2 |
g95 hello.f90 hello.f90をコンパイルし、リンクして実行可能ファイルa.outを生成します |
3 |
g95 -c h1.f90 h2.f90 h3.f90 複数のソースファイルをコンパイルします。 すべてうまくいけば、オブジェクトファイルh1.o、h2.oおよびh3.oが作成されます |
4 |
g95 -o hello h1.f90 h2.f90 h3.f90 複数のソースファイルをコンパイルし、「hello」という名前の実行可能ファイルにリンクします。 |
G95のコマンドラインオプション
-c Compile only, do not run the linker.
-o Specify the name of the output file, either an object file or the executable.
複数のソースファイルとオブジェクトファイルを一度に指定できます。 Fortranファイルは、「。f」、「。F」、「。for」、「。FOR」、「。f90」、「。F90」、「。f95」、「。F95」、「。」で終わる名前で示されます。 f03 "および" .F03 "。 複数のソースファイルを指定できます。 オブジェクトファイルも指定でき、リンクされて実行可能ファイルを形成します。
Fortran-基本構文
Fortranプログラムは、メインプログラム、モジュール、外部サブプログラムまたはプロシージャなどのプログラムユニットのコレクションで構成されています。
各プログラムには1つのメインプログラムが含まれ、他のプログラムユニットが含まれる場合と含まれない場合があります。 メインプログラムの構文は次のとおりです-
program program_name
implicit none
! type declaration statements
! executable statements
end program program_name
Fortranの簡単なプログラム
2つの数字を追加して結果を出力するプログラムを書きましょう-
program addNumbers
! This simple program adds two numbers
implicit none
! Type declarations
real :: a, b, result
! Executable statements
a = 12.0
b = 15.0
result = a + b
print *, 'The total is ', result
end program addNumbers
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
The total is 27.0000000
次のことに注意してください-
- すべてのFortranプログラムは、キーワード program で始まり、キーワード end program で終わり、その後にプログラムの名前が続きます。
- implicit none ステートメントにより、コンパイラはすべての変数タイプが適切に宣言されていることを確認できます。 すべてのプログラムの開始時に常に implicit none を使用する必要があります。
- Fortranのコメントは感嘆符(!)で始まります。これ以降のすべての文字(文字列を除く)はコンパイラによって無視されます。
- print *コマンドは、画面にデータを表示します。
- コード行のインデントは、プログラムを読みやすくするための良い習慣です。
- Fortranでは、大文字と小文字の両方を使用できます。 Fortranは、文字列リテラルを除き、大文字と小文字を区別しません。
基本
Fortranの*基本文字セット*には以下が含まれます-
- 文字A … Zと… z
- 数字0 … 9
- アンダースコア(_)文字
- 特殊文字=:+空白-*/()[]、。 $ '! "%&; <>?
- トークン*は、基本文字セットの文字で構成されています。 トークンは、キーワード、識別子、定数、文字列リテラル、またはシンボルです。
プログラム文はトークンで構成されています。
識別子
識別子は、変数、プロシージャ、またはその他のユーザー定義アイテムを識別するために使用される名前です。 Fortranの名前は、次の規則に従う必要があります-
- 31文字を超えることはできません。
- 英数字(アルファベットのすべての文字、0〜9の数字)とアンダースコア(_)で構成する必要があります。
- 名前の最初の文字は文字でなければなりません。
- 名前は大文字と小文字を区別しません
キーワード
キーワードは、言語専用の特別な単語です。 これらの予約語は、識別子または名前として使用できません。
次の表は、Fortranのキーワードを示しています-
非I/Oキーワード
割り当て可能
割り当てる
割り当てます
割り当て
ブロックデータ
call
case
キャラクター
一般
複雑な
含む
持続する
サイクル
data
割り当て解除
デフォルト
do
倍精度
else
そうでなければ
他の場所で
エンドブロックデータ
終了する
終了機能
もし終わったら
終了インターフェース
エンドモジュール
終了プログラム
選択終了
終了サブルーチン
エンドタイプ
どこで終わる
エントリ
等価
exit
外部
関数
に行く
if
暗黙
in
inout
整数
意図
インタフェース
固有の
kind
len
論理的
モジュール
ネームリスト
無効にする
only
オペレーター
オプション
out
パラメータ
一時停止
ポインタ
非公開
プログラム
パブリック
real
再帰的
結果
戻る
save
ケースを選択
stop
サブルーチン
ターゲット
then
type
タイプ()
use
どこで
しながら
I/O関連のキーワード
バックスペース
閉じる
エンドファイル
フォーマット
問い合わせる
open
印刷する
read
巻き戻す
書きます
Fortran-データ型
Fortranには5つの組み込みデータ型がありますが、独自のデータ型も派生できます。 5つの組み込み型は-
- 整数型
- リアルタイプ
- 複合型
- 論理型 *キャラクタータイプ
整数型
整数型は整数値のみを保持できます。 次の例では、通常の4バイト整数に保持できる最大値を抽出します-
program testingInt
implicit none
integer :: largeval
print* , huge(largeval)
end program testingInt
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
2147483647
- huge()関数は、特定の整数データ型が保持できる最大数を与えることに注意してください。 *kind 指定子を使用してバイト数を指定することもできます。 次の例はこれを示しています-
program testingInt
implicit none
!two byte integer
integer(kind = 2) :: shortval
!four byte integer
integer(kind = 4) :: longval
!eight byte integer
integer(kind = 8) :: verylongval
!sixteen byte integer
integer(kind = 16) :: veryverylongval
!default integer
integer :: defval
print *, huge(shortval)
print *, huge(longval)
print *, huge(verylongval)
print *, huge(veryverylongval)
print *, huge(defval)
end program testingInt
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
32767
2147483647
9223372036854775807
170141183460469231731687303715884105727
2147483647
リアルタイプ
2.0、3.1415、-100.876などの浮動小数点数を格納します。
伝統的に、デフォルトの real 型と double precision 型の2つの異なる実数型があります。
ただし、Fortran 90/95では、 kind 指定子を使用して実数および整数データ型の精度をより詳細に制御できます。これについては、数値の章で説明します。
次の例は、実際のデータ型の使用を示しています-
program division
implicit none
! Define real variables
real :: p, q, realRes
! Define integer variables
integer :: i, j, intRes
! Assigning values
p = 2.0
q = 3.0
i = 2
j = 3
! floating point division
realRes = p/q
intRes = i/j
print *, realRes
print *, intRes
end program division
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
0.666666687
0
複合型
これは、複素数を格納するために使用されます。 複素数には、実数部と虚数部の2つの部分があります。 2つの連続した数値ストレージユニットは、これら2つの部分を格納します。
たとえば、複素数(3.0、-5.0)は3.0 – 5.0iに等しい
数値の章で、複合型について詳しく説明します。
論理型
論理値は2つのみです:* .true。および .false。*
キャラクタータイプ
文字タイプには、文字と文字列が格納されます。 文字列の長さは、len指定子で指定できます。 長さを指定しない場合、1です。
例えば、
character (len = 40) :: name
name = “Zara Ali”
式* name(1:4)*は、サブストリング「Zara」を提供します。
暗黙的な入力
Fortranの古いバージョンでは、暗黙的な型指定と呼ばれる機能が許可されていました。つまり、使用する前に変数を宣言する必要はありません。 変数が宣言されていない場合、その名前の最初の文字がその型を決定します。
i、j、k、l、m、またはnで始まる変数名は整数変数であると見なされ、その他は実変数です。 ただし、すべての変数を宣言する必要があります。これは適切なプログラミング方法です。 そのためには、ステートメントでプログラムを開始します-
implicit none
このステートメントは、暗黙的な入力をオフにします。
Fortran-変数
変数は、プログラムが操作できるストレージ領域に付けられた名前に他なりません。 各変数には、変数のメモリのサイズとレイアウトを決定する特定のタイプが必要です。そのメモリ内に保存できる値の範囲。変数に適用できる一連の操作。
変数の名前は、文字、数字、およびアンダースコア文字で構成できます。 Fortranの名前は、次の規則に従う必要があります-
- 31文字を超えることはできません。
- 英数字(アルファベットのすべての文字、0〜9の数字)とアンダースコア(_)で構成する必要があります。
- 名前の最初の文字は文字でなければなりません。 *名前は大文字と小文字を区別しません。
前の章で説明した基本的な型に基づいて、変数の型は次のとおりです-
Sr.No | Type & Description |
---|---|
1 |
整数値のみを保持できます。 |
2 |
Real 浮動小数点数を格納します。 |
3 |
Complex 複素数の保存に使用されます。 |
4 |
Logical 論理ブール値を保存します。 |
5 |
Character 文字または文字列を保存します。 |
可変宣言
変数は、型宣言ステートメントでプログラム(またはサブプログラム)の先頭で宣言されます。
変数宣言の構文は次のとおりです-
type-specifier :: variable_name
例えば
integer :: total
real :: average
complex :: cx
logical :: done
character(len = 80) :: message ! a string of 80 characters
後で、これらの変数に値を割り当てることができます。たとえば、
total = 20000
average = 1666.67
done = .true.
message = “A big Hello from Tutorials Point”
cx = (3.0, 5.0) ! cx = 3.0 + 5.0i
また、組み込み関数* cmplx、*を使用して、値を複素変数に割り当てることができます-
cx = cmplx (1.0/2.0, -7.0) ! cx = 0.5 – 7.0i
cx = cmplx (x, y) ! cx = x + yi
例
次の例は、変数の宣言、割り当て、画面上の表示を示しています-
program variableTesting
implicit none
! declaring variables
integer :: total
real :: average
complex :: cx
logical :: done
character(len=80) :: message ! a string of 80 characters
!assigning values
total = 20000
average = 1666.67
done = .true.
message = "A big Hello from Tutorials Point"
cx = (3.0, 5.0) ! cx = 3.0 + 5.0i
Print *, total
Print *, average
Print *, cx
Print *, done
Print *, message
end program variableTesting
上記のコードをコンパイルして実行すると、次の結果が生成されます-
20000
1666.67004
(3.00000000, 5.00000000 )
T
A big Hello from Tutorials Point
Fortran-定数
定数は、プログラムが実行中に変更できない固定値を参照します。 これらの固定値は*リテラル*とも呼ばれます。
定数は、整数定数、浮動定数、文字定数、複素数定数、または文字列リテラルなどの基本的なデータ型のいずれかです。 論理定数は2つのみです:* .true。および .false。*
定数は、通常の変数と同様に扱われますが、定義後に値を変更することはできません。
名前付き定数とリテラル
定数には2種類あります-
- リテラル定数 *名前付き定数
リテラル定数には値がありますが、名前はありません。
たとえば、リテラル定数は次のとおりです-
Type | Example |
---|---|
Integer constants | 0 1 -1 300 123456789 |
Real constants | 0.0 1.0 -1.0 123.456 7.1E+10 -52.715E-30 |
Complex constants | (0.0, 0.0) (-123.456E+30, 987.654E-29) |
Logical constants | .true. .false. |
Character constants |
"PQR" "a" "123’abc$%#@!" " 見積もり "" " 「PQR」「a」「123」abc $%#@!」 'アポストロフィ' |
名前付き定数には、値と名前があります。
名前付き定数は、変数の型宣言と同様に、プログラムまたはプロシージャの先頭で宣言し、その名前と型を示す必要があります。 名前付き定数は、パラメータ属性で宣言されます。 例えば、
real, parameter :: pi = 3.1415927
例
次のプログラムは、重力下での垂直運動による変位を計算します。
program gravitationalDisp
! this program calculates vertical motion under gravity
implicit none
! gravitational acceleration
real, parameter :: g = 9.81
! variable declaration
real :: s ! displacement
real :: t ! time
real :: u ! initial speed
! assigning values
t = 5.0
u = 50
! displacement
s = u* t - g * (t**2)/2
! output
print *, "Time = ", t
print *, 'Displacement = ',s
end program gravitationalDisp
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Time = 5.00000000
Displacement = 127.374992
Fortran-演算子
演算子は、特定の数学的または論理的な操作を実行するようコンパイラーに指示する記号です。 Fortranは、次の種類の演算子を提供します-
- 算術演算子
- 関係演算子
- 論理演算子
これらすべてのタイプの演算子を1つずつ見ていきましょう。
算術演算子
次の表は、Fortranでサポートされているすべての算術演算子を示しています。 変数 A が5を保持し、変数 B が3を保持すると仮定します-
リンク:/fortran/fortran_arithmetic_operators [例を表示]
Operator | Description | Example |
---|---|---|
+ | Addition Operator, adds two operands. | A + B will give 8 |
- | Subtraction Operator, subtracts second operand from the first. | A - B will give 2 |
* | Multiplication Operator, multiplies both operands. | A* B will give 15 |
/ | Division Operator, divides numerator by de-numerator. | A/B will give 1 |
* * | Exponentiation Operator, raises one operand to the power of the other. | A* * B will give 125 |
関係演算子
次の表は、Fortranでサポートされているすべての関係演算子を示しています。 変数 A が10を保持し、変数 B が20を保持すると仮定します-
リンク:/fortran/fortran_relational_operators [例を表示]
Operator | Equivalent | Description | Example |
---|---|---|---|
== | .eq. | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
/= | .ne. | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | .gt. | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | .lt. | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | .ge. | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
⇐ | .le. | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A ⇐ B) is true. |
論理演算子
Fortranの論理演算子は、論理値.trueでのみ機能します。 および.false。
次の表は、Fortranでサポートされているすべての論理演算子を示しています。 変数Aが.trueを保持していると仮定します。 変数Bは.falseを保持します。 、その後-
リンク:/fortran/fortran_logical_operators [例を表示]
Operator | Description | Example |
---|---|---|
.and. | Called Logical AND operator. If both the operands are non-zero, then condition becomes true. | (A .and. B) is false. |
.or. | Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. | (A .or. B) is true. |
.not. | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A .and. B) is true. |
.eqv. | Called Logical EQUIVALENT Operator. Used to check equivalence of two logical values. | (A .eqv. B) is false. |
.neqv. | Called Logical NON-EQUIVALENT Operator. Used to check non-equivalence of two logical values. | (A .neqv. B) is true. |
Fortranでの演算子の優先順位
演算子の優先順位は、式内の用語のグループ化を決定します。 これは、式の評価方法に影響します。 特定の演算子は、他の演算子よりも優先順位が高くなっています。たとえば、乗算演算子は加算演算子よりも優先順位が高くなります。
たとえば、x = 7 + 3 * 2;ここでは、演算子*の優先順位が+よりも高いため、xには20ではなく13が割り当てられます。したがって、最初に3 * 2で乗算され、7に加算されます。
ここでは、優先順位が最も高い演算子が表の上部に表示され、優先順位が最も低い演算子が下部に表示されます。 式内では、優先順位の高い演算子が最初に評価されます。
リンク:/fortran/fortran_operators_precedence [例を表示]
Category | Operator | Associativity |
---|---|---|
Logical NOT and negative sign | .not. (-) | Left to right |
Exponentiation | ** | Left to right |
Multiplicative | */ | Left to right |
Additive | + - | Left to right |
Relational | < ⇐ > >= | Left to right |
Equality | ==/= | Left to right |
Logical AND | .and. | Left to right |
Logical OR | .or. | Left to right |
Assignment | = | Right to left |
Fortran-決定
意思決定構造では、プログラマーは、プログラムが評価またはテストする1つ以上の条件を、条件が真であると判断された場合に実行されるステートメントとともに指定する必要があります。条件は偽と判断されます。
以下は、ほとんどのプログラミング言語で見られる典型的な意思決定構造の一般的な形式です-
Fortranは、次のタイプの意思決定構造を提供します。
Sr.No | Statement & Description |
---|---|
1 |
|
2 |
「 if…then ステートメント」の後に、オプションの「* elseステートメント」を続けることができます。これは、論理式が偽の場合に実行されます。 |
3 |
|
4 |
ある* if または else if 文を別の* if またはor * else if 文の中に使用できます。 |
5 |
「ケースを選択」ステートメントを使用すると、値のリストに対する変数の等価性をテストできます。 |
6 |
ある select case ステートメントを別の select case ステートメント内で使用できます。 |
Fortran-ループ
コードのブロックを数回実行する必要がある場合があります。 一般に、ステートメントは順番に実行されます。関数の最初のステートメントが最初に実行され、次に2番目のステートメントが実行されます。
プログラミング言語は、より複雑な実行パスを可能にするさまざまな制御構造を提供します。
ループステートメントを使用すると、ステートメントまたはステートメントのグループを複数回実行できます。ほとんどのプログラミング言語では、ループステートメントの一般的な形式は次のとおりです-
Fortranは、ループ要件を処理するために次のタイプのループ構造を提供します。 詳細を確認するには、次のリンクをクリックしてください。
Sr.No | Loop Type & Description |
---|---|
1 |
このコンストラクトを使用すると、特定の条件が満たされている間、1つまたは一連のステートメントを繰り返し実行できます。 |
2 |
特定の条件が真の間、ステートメントまたはステートメントのグループを繰り返します。 ループ本体を実行する前に条件をテストします。 |
3 |
他のループ構造内で1つ以上のループ構造を使用できます。 |
ループ制御ステートメント
ループ制御ステートメントは、通常のシーケンスから実行を変更します。 実行がスコープを離れると、そのスコープで作成されたすべての自動オブジェクトが破棄されます。
Fortranは、次の制御ステートメントをサポートしています。 詳細を確認するには、次のリンクをクリックしてください。
Sr.No | Control Statement & Description |
---|---|
1 |
exitステートメントが実行されると、ループは終了し、end doステートメントの後の最初の実行可能ステートメントでプログラムの実行が継続されます。 |
2 |
サイクル文が実行されると、プログラムは次の反復の開始から続行します。 |
3 |
プログラムの実行を停止する場合は、停止ステートメントを挿入できます |
Fortran-数値
Fortranの数は3つの組み込みデータ型で表されます-
- 整数型
- リアルタイプ *複合型
整数型
整数型は整数値のみを保持できます。 次の例では、通常の4バイト整数で保持できる最大値を抽出します-
program testingInt
implicit none
integer :: largeval
print* , huge(largeval)
end program testingInt
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
2147483647
- huge()関数は、特定の整数データ型が保持できる最大数を与えることに注意してください。 *kind 指定子を使用してバイト数を指定することもできます。 次の例はこれを示しています-
program testingInt
implicit none
!two byte integer
integer(kind = 2) :: shortval
!four byte integer
integer(kind = 4) :: longval
!eight byte integer
integer(kind = 8) :: verylongval
!sixteen byte integer
integer(kind = 16) :: veryverylongval
!default integer
integer :: defval
print *, huge(shortval)
print *, huge(longval)
print *, huge(verylongval)
print *, huge(veryverylongval)
print *, huge(defval)
end program testingInt
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
32767
2147483647
9223372036854775807
170141183460469231731687303715884105727
2147483647
リアルタイプ
2.0、3.1415、-100.876などの浮動小数点数を格納します。
従来、2つの異なる real 型がありました。デフォルトのreal型と double precision 型です。
ただし、Fortran 90/95では、 kind 指定子を使用して、実データ型と整数データ型の精度をより詳細に制御できます。これについては、後ほど説明します。
次の例は、実際のデータ型の使用を示しています-
program division
implicit none
! Define real variables
real :: p, q, realRes
! Define integer variables
integer :: i, j, intRes
! Assigning values
p = 2.0
q = 3.0
i = 2
j = 3
! floating point division
realRes = p/q
intRes = i/j
print *, realRes
print *, intRes
end program division
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
0.666666687
0
複合型
これは、複素数を格納するために使用されます。 複素数には、実部と虚部の2つの部分があります。 2つの連続した数値ストレージユニットは、これら2つの部分を格納します。
たとえば、複素数(3.0、-5.0)は3.0 – 5.0iに等しい
汎用関数* cmplx()*は複素数を作成します。 入力引数のタイプに関係なく、実部と虚部が単精度である結果を生成します。
program createComplex
implicit none
integer :: i = 10
real :: x = 5.17
print *, cmplx(i, x)
end program createComplex
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
(10.0000000, 5.17000008)
次のプログラムは、複素数演算を示しています-
program ComplexArithmatic
implicit none
complex, parameter :: i = (0, 1) ! sqrt(-1)
complex :: x, y, z
x = (7, 8);
y = (5, -7)
write(*,*) i *x* y
z = x + y
print *, "z = x + y = ", z
z = x - y
print *, "z = x - y = ", z
z = x *y
print* , "z = x *y = ", z
z = x/y
print* , "z = x/y = ", z
end program ComplexArithmatic
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
(9.00000000, 91.0000000)
z = x + y = (12.0000000, 1.00000000)
z = x - y = (2.00000000, 15.0000000)
z = x * y = (91.0000000, -9.00000000)
z = x/y = (-0.283783793, 1.20270276)
数値の範囲、精度、サイズ
整数の範囲、浮動小数点数の精度とサイズは、特定のデータ型に割り当てられたビット数に依存します。
次の表は、整数のビット数と範囲を示しています-
Number of bits | Maximum value | Reason |
---|---|---|
64 | 9,223,372,036,854,774,807 | (2**63)–1 |
32 | 2,147,483,647 | (2**31)–1 |
次の表は、ビット数、最小値と最大値、および実数の精度を示しています。
Number of bits | Largest value | Smallest value | Precision |
---|---|---|---|
64 | 0.8E+308 | 0.5E–308 | 15–18 |
32 | 1.7E+38 | 0.3E–38 | 6-9 |
次の例はこれを示しています-
program rangePrecision
implicit none
real:: x, y, z
x = 1.5e+40
y = 3.73e+40
z = x *y
print* , z
end program rangePrecision
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
x = 1.5e+40
1
Error : Real constant overflows its kind at (1)
main.f95:5.12:
y = 3.73e+40
1
Error : Real constant overflows its kind at (1)
今、私たちはより小さい数を使用しましょう-
program rangePrecision
implicit none
real:: x, y, z
x = 1.5e+20
y = 3.73e+20
z = x *y
print* , z
z = x/y
print *, z
end program rangePrecision
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Infinity
0.402144760
アンダーフローを見てみましょう-
program rangePrecision
implicit none
real:: x, y, z
x = 1.5e-30
y = 3.73e-60
z = x *y
print* , z
z = x/y
print *, z
end program rangePrecision
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
y = 3.73e-60
1
Warning : Real constant underflows its kind at (1)
Executing the program....
$demo
0.00000000E+00
Infinity
種類指定子
科学プログラミングでは、作業が行われているハードウェアプラットフォームのデータの範囲と精度を知る必要があります。
組み込み関数* kind()*を使用すると、プログラムを実行する前にハードウェアのデータ表現の詳細を照会できます。
program kindCheck
implicit none
integer :: i
real :: r
complex :: cp
print *,' Integer ', kind(i)
print *,' Real ', kind(r)
print *,' Complex ', kind(cp)
end program kindCheck
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Integer 4
Real 4
Complex 4
また、すべてのデータ型の種類を確認することができます-
program checkKind
implicit none
integer :: i
real :: r
character :: c
logical :: lg
complex :: cp
print *,' Integer ', kind(i)
print *,' Real ', kind(r)
print *,' Complex ', kind(cp)
print *,' Character ', kind(c)
print *,' Logical ', kind(lg)
end program checkKind
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Integer 4
Real 4
Complex 4
Character 1
Logical 4
Fortran-文字
Fortran言語では、文字を単一の文字または連続した文字列として扱うことができます。
文字は、基本的な文字セット、つまり、文字、10進数、アンダースコア、21個の特殊文字から取られた任意の記号です。
文字定数は、固定値の文字列です。
組み込みデータ型 character は、文字と文字列を格納します。 文字列の長さは、 len 指定子で指定できます。 長さを指定しない場合、1です。 位置によって参照する文字列内の個々の文字を参照できます。左端の文字は位置1です。
キャラクター宣言
文字型データの宣言は他の変数と同じです-
type-specifier :: variable_name
例えば、
character :: reply, sex
次のような値を割り当てることができます
reply = ‘N’
sex = ‘F’
次の例は、文字データ型の宣言と使用を示しています-
program hello
implicit none
character(len = 15) :: surname, firstname
character(len = 6) :: title
character(len = 25)::greetings
title = 'Mr. '
firstname = 'Rowan '
surname = 'Atkinson'
greetings = 'A big hello from Mr. Bean'
print *, 'Here is ', title, firstname, surname
print *, greetings
end program hello
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Here is Mr. Rowan Atkinson
A big hello from Mr. Bean
文字の連結
連結演算子//は、文字を連結します。
次の例はこれを示しています-
program hello
implicit none
character(len = 15) :: surname, firstname
character(len = 6) :: title
character(len = 40):: name
character(len = 25)::greetings
title = 'Mr. '
firstname = 'Rowan '
surname = 'Atkinson'
name = title//firstname//surname
greetings = 'A big hello from Mr. Bean'
print *, 'Here is ', name
print *, greetings
end program hello
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Here is Mr.Rowan Atkinson
A big hello from Mr.Bean
いくつかのキャラクター関数
次の表は、説明とともにいくつかの一般的に使用される文字関数を示しています-
Sr.No | Function & Description |
---|---|
1 |
len(string) 文字列の長さを返します |
2 |
index(string,sustring) 別の文字列内の部分文字列の位置を見つけ、見つからない場合は0を返します。 |
3 |
achar(int) 整数を文字に変換します |
4 |
iachar(c) 文字を整数に変換します |
5 |
trim(string) 末尾の空白を削除した文字列を返します。 |
6 |
scan(string, chars) 「chars」に含まれる文字の最初の出現を「文字列」で左から右に(back = .trueでない限り)検索します。 その文字の位置を示す整数を返します。「chars」の文字が見つからない場合はゼロを返します。 |
7 |
verify(string, chars) 「chars」に含まれていない文字が最初に出現する場合、「string」を左から右に(back = .trueでない限り)スキャンします。 その文字の位置を示す整数を返します。「chars」の文字のみが見つかった場合はゼロを返します |
8 |
adjustl(string) 「文字列」に含まれる文字を左揃えにします |
9 |
adjustr(string) 「文字列」に含まれる文字を右揃えします |
10 |
len_trim(string) 「string」の長さ(len(string))から末尾ブランクの数を引いた値に等しい整数を返します |
11 |
repeat(string,ncopy) これは、「ncopy」に「string」の長さを掛けた長さのストリングを返し、「string」の「ncopy」連結コピーを含みます |
例1
この例は、*インデックス*関数の使用を示しています-
program testingChars
implicit none
character (80) :: text
integer :: i
text = 'The intrinsic data type character stores characters and strings.'
i=index(text,'character')
if (i/= 0) then
print *, ' The word character found at position ',i
print *, ' in text: ', text
end if
end program testingChars
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
The word character found at position 25
in text : The intrinsic data type character stores characters and strings.
例2
この例は、 trim 関数の使用方法を示しています-
program hello
implicit none
character(len = 15) :: surname, firstname
character(len = 6) :: title
character(len = 25)::greetings
title = 'Mr.'
firstname = 'Rowan'
surname = 'Atkinson'
print *, 'Here is', title, firstname, surname
print *, 'Here is', trim(title),' ',trim(firstname),' ', trim(surname)
end program hello
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Here isMr. Rowan Atkinson
Here isMr. Rowan Atkinson
実施例3
この例は、 achar 関数の使用を示しています-
program testingChars
implicit none
character:: ch
integer:: i
do i = 65, 90
ch = achar(i)
print*, i, ' ', ch
end do
end program testingChars
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
65 A
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
文字の字句順を確認する
次の関数は、文字の字句シーケンスを決定します-
Sr.No | Function & Description |
---|---|
1 |
lle(char, char) 最初の文字が字句的に2番目の文字以下かどうかを比較します |
2 |
lge(char, char) 最初の文字が字句的に2番目の文字以上かどうかを比較します |
3 |
lgt(char, char) 最初の文字が2番目の文字より字句的に大きいかどうかを比較します |
4 |
llt(char, char) 最初の文字が2番目の文字より字句的に小さいかどうかを比較します |
例4
次の機能は、使用を示しています-
program testingChars
implicit none
character:: a, b, c
a = 'A'
b = 'a'
c = 'B'
if(lgt(a,b)) then
print *, 'A is lexically greater than a'
else
print *, 'a is lexically greater than A'
end if
if(lgt(a,c)) then
print *, 'A is lexically greater than B'
else
print *, 'B is lexically greater than A'
end if
if(llt(a,b)) then
print *, 'A is lexically less than a'
end if
if(llt(a,c)) then
print *, 'A is lexically less than B'
end if
end program testingChars
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
a is lexically greater than A
B is lexically greater than A
A is lexically less than a
A is lexically less than B
Fortran-ストリング
Fortran言語では、文字を単一の文字または連続した文字列として扱うことができます。
文字列の長さは1文字のみの場合もあれば、長さがゼロの場合もあります。 Fortranでは、文字定数は二重引用符または単一引用符のペアの間に指定されます。
組み込みデータ型 character は、文字と文字列を格納します。 文字列の長さは* len指定子*で指定できます。 長さを指定しない場合、1です。 位置によって参照する文字列内の個々の文字を参照できます。左端の文字は位置1です。
文字列宣言
文字列を宣言することは他の変数と同じです-
type-specifier :: variable_name
例えば、
Character(len = 20) :: firstname, surname
次のような値を割り当てることができます
character (len = 40) :: name
name = “Zara Ali”
次の例は、文字データ型の宣言と使用を示しています-
program hello
implicit none
character(len = 15) :: surname, firstname
character(len = 6) :: title
character(len = 25)::greetings
title = 'Mr.'
firstname = 'Rowan'
surname = 'Atkinson'
greetings = 'A big hello from Mr. Beans'
print *, 'Here is', title, firstname, surname
print *, greetings
end program hello
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Here isMr. Rowan Atkinson
A big hello from Mr. Bean
文字列連結
連結演算子//は、文字列を連結します。
次の例はこれを示しています-
program hello
implicit none
character(len = 15) :: surname, firstname
character(len = 6) :: title
character(len = 40):: name
character(len = 25)::greetings
title = 'Mr.'
firstname = 'Rowan'
surname = 'Atkinson'
name = title//firstname//surname
greetings = 'A big hello from Mr. Beans'
print *, 'Here is', name
print *, greetings
end program hello
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Here is Mr. Rowan Atkinson
A big hello from Mr. Bean
部分文字列の抽出
Fortranでは、文字列にインデックスを付けて、文字列から部分文字列を抽出し、部分文字列の開始インデックスと終了インデックスを1組の角かっこで指定できます。 これはエクステント指定子と呼ばれます。
次の例は、文字列「hello world」から部分文字列「world」を抽出する方法を示しています-
program subString
character(len = 11)::hello
hello = "Hello World"
print*, hello(7:11)
end program subString
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
World
例
次の例では、 date_and_time 関数を使用して、日付と時刻の文字列を指定します。 範囲指定子を使用して、年、日付、月、時間、分、および秒の情報を個別に抽出します。
program datetime
implicit none
character(len = 8) :: dateinfo ! ccyymmdd
character(len = 4) :: year, month*2, day*2
character(len = 10) :: timeinfo ! hhmmss.sss
character(len = 2) :: hour, minute, second*6
call date_and_time(dateinfo, timeinfo)
! let’s break dateinfo into year, month and day.
! dateinfo has a form of ccyymmdd, where cc = century, yy = year
! mm = month and dd = day
year = dateinfo(1:4)
month = dateinfo(5:6)
day = dateinfo(7:8)
print*, 'Date String:', dateinfo
print*, 'Year:', year
print *,'Month:', month
print *,'Day:', day
! let’s break timeinfo into hour, minute and second.
! timeinfo has a form of hhmmss.sss, where h = hour, m = minute
! and s = second
hour = timeinfo(1:2)
minute = timeinfo(3:4)
second = timeinfo(5:10)
print*, 'Time String:', timeinfo
print*, 'Hour:', hour
print*, 'Minute:', minute
print*, 'Second:', second
end program datetime
上記のプログラムをコンパイルして実行すると、詳細な日時情報が得られます-
Date String: 20140803
Year: 2014
Month: 08
Day: 03
Time String: 075835.466
Hour: 07
Minute: 58
Second: 35.466
ストリングのトリミング
*trim* 関数は文字列を取り、すべての後続ブランクを削除した後に入力文字列を返します。
例
program trimString
implicit none
character (len = *), parameter :: fname="Susanne", sname="Rizwan"
character (len = 20) :: fullname
fullname = fname//" "//sname !concatenating the strings
print*,fullname,", the beautiful dancer from the east!"
print*,trim(fullname),", the beautiful dancer from the east!"
end program trimString
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Susanne Rizwan , the beautiful dancer from the east!
Susanne Rizwan, the beautiful dancer from the east!
弦の左右調整
関数 adjustl は文字列を取得し、先頭の空白を削除して末尾の空白として追加することで返します。
関数 adjustr は文字列を受け取り、末尾の空白を削除して先頭の空白として追加することでそれを返します。
例
program hello
implicit none
character(len = 15) :: surname, firstname
character(len = 6) :: title
character(len = 40):: name
character(len = 25):: greetings
title = 'Mr. '
firstname = 'Rowan'
surname = 'Atkinson'
greetings = 'A big hello from Mr. Beans'
name = adjustl(title)//adjustl(firstname)//adjustl(surname)
print *, 'Here is', name
print *, greetings
name = adjustr(title)//adjustr(firstname)//adjustr(surname)
print *, 'Here is', name
print *, greetings
name = trim(title)//trim(firstname)//trim(surname)
print *, 'Here is', name
print *, greetings
end program hello
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Here is Mr. Rowan Atkinson
A big hello from Mr. Bean
Here is Mr. Rowan Atkinson
A big hello from Mr. Bean
Here is Mr.RowanAtkinson
A big hello from Mr. Bean
文字列内の部分文字列を検索する
インデックス関数は2つの文字列を取り、2番目の文字列が最初の文字列の部分文字列かどうかを確認します。 2番目の引数が最初の引数の部分文字列の場合、最初の文字列の2番目の文字列の開始インデックスである整数を返します。それ以外の場合はゼロを返します。
例
program hello
implicit none
character(len=30) :: myString
character(len=10) :: testString
myString = 'This is a test'
testString = 'test'
if(index(myString, testString) == 0)then
print *, 'test is not found'
else
print *, 'test is found at index: ', index(myString, testString)
end if
end program hello
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
test is found at index: 11
Fortran-配列
配列は、同じタイプの要素の固定サイズの順次コレクションを格納できます。 配列はデータのコレクションを格納するために使用されますが、配列を同じタイプの変数のコレクションと考える方が便利な場合がよくあります。
すべての配列は、連続したメモリ位置で構成されています。 最下位アドレスは最初の要素に対応し、最上位アドレスは最後の要素に対応します。
Numbers(1) | Numbers(2) | Numbers(3) | Numbers(4) | … |
配列は1次元(ベクトルなど)、2次元(行列など)で、Fortranでは最大7次元の配列を作成できます。
配列の宣言
配列は dimension 属性で宣言されます。
たとえば、5つの要素を含む実数のnumberという名前の1次元配列を宣言するには、次のように記述します。
real, dimension(5) :: numbers
配列の個々の要素は、添え字を指定することにより参照されます。 配列の最初の要素の添字は1です。 配列番号には、numbers(1)、numbers(2)、numbers(3)、numbers(4)、numbers(5)の5つの実変数が含まれています。
matrixという名前の整数の5 x 5の2次元配列を作成するには、次のように記述します-
integer, dimension (5,5) :: matrix
また、例えば、いくつかの明示的な下限を持つ配列を宣言することができます-
real, dimension(2:6) :: numbers
integer, dimension (-3:2,0:4) :: matrix
値の割り当て
次のように、個々のメンバーに値を割り当てることができます。
numbers(1) = 2.0
または、ループを使用できます。
do i =1,5
numbers(i) = i * 2.0
end do
1次元配列要素には、配列コンストラクターと呼ばれる短い記号を使用して、値を直接割り当てることができます。
numbers = (/1.5, 3.2,4.5,0.9,7.2/)
- 括弧「(」とバックスラッシュ「/」の間にスペースを入れないでください *
例
次の例は、上記の概念を示しています。
program arrayProg
real :: numbers(5) !one dimensional integer array
integer :: matrix(3,3), i , j !two dimensional real array
!assigning some values to the array numbers
do i=1,5
numbers(i) = i* 2.0
end do
!display the values
do i = 1, 5
Print *, numbers(i)
end do
!assigning some values to the array matrix
do i=1,3
do j = 1, 3
matrix(i, j) = i+j
end do
end do
!display the values
do i=1,3
do j = 1, 3
Print *, matrix(i,j)
end do
end do
!short hand assignment
numbers = (/1.5, 3.2,4.5,0.9,7.2/)
!display the values
do i = 1, 5
Print *, numbers(i)
end do
end program arrayProg
上記のコードをコンパイルして実行すると、次の結果が生成されます-
2.00000000
4.00000000
6.00000000
8.00000000
10.0000000
2
3
4
3
4
5
4
5
6
1.50000000
3.20000005
4.50000000
0.899999976
7.19999981
いくつかのアレイ関連用語
次の表は、いくつかの配列関連の用語を示しています-
Term | Meaning |
---|---|
Rank | It is the number of dimensions an array has. For example, for the array named matrix, rank is 2, and for the array named numbers, rank is 1. |
Extent | It is the number of elements along a dimension. For example, the array numbers has extent 5 and the array named matrix has extent 3 in both dimensions. |
Shape | The shape of an array is a one-dimensional integer array, containing the number of elements (the extent) in each dimension. For example, for the array matrix, shape is (3, 3) and the array numbers it is (5). |
Size | It is the number of elements an array contains. For the array matrix, it is 9, and for the array numbers, it is 5. |
配列をプロシージャに渡す
配列を引数としてプロシージャに渡すことができます。 次の例は、概念を示しています-
program arrayToProcedure
implicit none
integer, dimension (5) :: myArray
integer :: i
call fillArray (myArray)
call printArray(myArray)
end program arrayToProcedure
subroutine fillArray (a)
implicit none
integer, dimension (5), intent (out) :: a
! local variables
integer :: i
do i = 1, 5
a(i) = i
end do
end subroutine fillArray
subroutine printArray(a)
integer, dimension (5) :: a
integer::i
do i = 1, 5
Print *, a(i)
end do
end subroutine printArray
上記のコードをコンパイルして実行すると、次の結果が生成されます-
1
2
3
4
5
上記の例では、サブルーチンfillArrayおよびprintArrayは、次元5の配列でのみ呼び出すことができます。 ただし、任意のサイズの配列に使用できるサブルーチンを作成するには、次の手法を使用して書き換えることができます-
program arrayToProcedure
implicit none
integer, dimension (10) :: myArray
integer :: i
interface
subroutine fillArray (a)
integer, dimension(:), intent (out) :: a
integer :: i
end subroutine fillArray
subroutine printArray (a)
integer, dimension(:) :: a
integer :: i
end subroutine printArray
end interface
call fillArray (myArray)
call printArray(myArray)
end program arrayToProcedure
subroutine fillArray (a)
implicit none
integer,dimension (:), intent (out) :: a
! local variables
integer :: i, arraySize
arraySize = size(a)
do i = 1, arraySize
a(i) = i
end do
end subroutine fillArray
subroutine printArray(a)
implicit none
integer,dimension (:) :: a
integer::i, arraySize
arraySize = size(a)
do i = 1, arraySize
Print *, a(i)
end do
end subroutine printArray
プログラムは size 関数を使用して配列のサイズを取得していることに注意してください。
上記のコードをコンパイルして実行すると、次の結果が生成されます-
1
2
3
4
5
6
7
8
9
10
配列セクション
これまで配列全体を参照してきましたが、Fortranは単一のステートメントを使用して、複数の要素または配列のセクションを参照する簡単な方法を提供します。
配列セクションにアクセスするには、セクションの下限と上限、およびすべての次元のストライド(増分)を指定する必要があります。 この表記法は*添字トリプレット:*と呼ばれます
array ([lower]:[upper][:stride], ...)
下限と上限が指定されていない場合、デフォルトは宣言した範囲になり、ストライド値はデフォルトで1になります。
次の例は、概念を示しています-
program arraySubsection
real, dimension(10) :: a, b
integer:: i, asize, bsize
a(1:7) = 5.0 ! a(1) to a(7) assigned 5.0
a(8:) = 0.0 ! rest are 0.0
b(2:10:2) = 3.9
b(1:9:2) = 2.5
!display
asize = size(a)
bsize = size(b)
do i = 1, asize
Print *, a(i)
end do
do i = 1, bsize
Print *, b(i)
end do
end program arraySubsection
上記のコードをコンパイルして実行すると、次の結果が生成されます-
5.00000000
5.00000000
5.00000000
5.00000000
5.00000000
5.00000000
5.00000000
0.00000000E+00
0.00000000E+00
0.00000000E+00
2.50000000
3.90000010
2.50000000
3.90000010
2.50000000
3.90000010
2.50000000
3.90000010
2.50000000
3.90000010
配列組み込み関数
Fortran 90/95は、いくつかの組み込み手続きを提供します。 これらは7つのカテゴリに分類できます。
- リンク:/fortran/vector_and_matrix_multiplication [ベクトルと行列の乗算]
- リンク:/fortran/reduction [Reduction]
- リンク:/fortran/inquiry [お問い合わせ]
- リンク:/fortran/construction [構築]
- リンク:/fortran/reshape [Reshape]
- リンク:/fortran/manipulation [操作]
- リンク:/fortran/location [場所]
Fortran-動的配列
- 動的配列*は、コンパイル時にはサイズが不明ですが、実行時には既知の配列です。
動的配列は、 allocatable 属性で宣言されます。
例えば、
real, dimension (:,:), allocatable :: darray
ただし、そのような配列にメモリを割り当てるには、 allocate 関数を使用して、配列のランク、つまり次元を指定する必要があります。
allocate ( darray(s1,s2) )
配列が使用された後、プログラムで、作成されたメモリは deallocate 関数を使用して解放する必要があります
deallocate (darray)
例
次の例は、上記の概念を示しています。
program dynamic_array
implicit none
!rank is 2, but size not known
real, dimension (:,:), allocatable :: darray
integer :: s1, s2
integer :: i, j
print*, "Enter the size of the array:"
read*, s1, s2
! allocate memory
allocate ( darray(s1,s2) )
do i = 1, s1
do j = 1, s2
darray(i,j) = i*j
print*, "darray(",i,",",j,") = ", darray(i,j)
end do
end do
deallocate (darray)
end program dynamic_array
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Enter the size of the array: 3,4
darray( 1 , 1 ) = 1.00000000
darray( 1 , 2 ) = 2.00000000
darray( 1 , 3 ) = 3.00000000
darray( 1 , 4 ) = 4.00000000
darray( 2 , 1 ) = 2.00000000
darray( 2 , 2 ) = 4.00000000
darray( 2 , 3 ) = 6.00000000
darray( 2 , 4 ) = 8.00000000
darray( 3 , 1 ) = 3.00000000
darray( 3 , 2 ) = 6.00000000
darray( 3 , 3 ) = 9.00000000
darray( 3 , 4 ) = 12.0000000
データ文の使用
*data* ステートメントは、複数の配列の初期化、または配列セクションの初期化に使用できます。
データ文の構文は次のとおりです-
data variable/list/...
例
次の例は、概念を示しています-
program dataStatement
implicit none
integer :: a(5), b(3,3), c(10),i, j
data a/7,8,9,10,11/
data b(1,:)/1,1,1/
data b(2,:)/2,2,2/
data b(3,:)/3,3,3/
data (c(i),i = 1,10,2)/4,5,6,7,8/
data (c(i),i = 2,10,2)/5*2/
Print *, 'The A array:'
do j = 1, 5
print*, a(j)
end do
Print *, 'The B array:'
do i = lbound(b,1), ubound(b,1)
write(*,*) (b(i,j), j = lbound(b,2), ubound(b,2))
end do
Print *, 'The C array:'
do j = 1, 10
print*, c(j)
end do
end program dataStatement
上記のコードをコンパイルして実行すると、次の結果が生成されます-
The A array:
7
8
9
10
11
The B array:
1 1 1
2 2 2
3 3 3
The C array:
4
2
5
2
6
2
7
2
8
2
Whereステートメントの使用
*where* ステートメントを使用すると、論理条件の結果に応じて、式で配列の一部の要素を使用できます。 指定された条件が真の場合、要素で式を実行できます。
例
次の例は、概念を示しています-
program whereStatement
implicit none
integer :: a(3,5), i , j
do i = 1,3
do j = 1, 5
a(i,j) = j-i
end do
end do
Print *, 'The A array:'
do i = lbound(a,1), ubound(a,1)
write(*,*) (a(i,j), j = lbound(a,2), ubound(a,2))
end do
where( a<0 )
a = 1
elsewhere
a = 5
end where
Print *, 'The A array:'
do i = lbound(a,1), ubound(a,1)
write(*,*) (a(i,j), j = lbound(a,2), ubound(a,2))
end do
end program whereStatement
上記のコードをコンパイルして実行すると、次の結果が生成されます-
The A array:
0 1 2 3 4
-1 0 1 2 3
-2 -1 0 1 2
The A array:
5 5 5 5 5
1 5 5 5 5
1 1 5 5 5
Fortran-派生データ型
Fortranでは、派生データ型を定義できます。 派生データ型は構造体とも呼ばれ、さまざまな型のデータオブジェクトで構成できます。
派生データ型は、レコードを表すために使用されます。 E.g. あなたは図書館であなたの本を追跡したい場合は、各本に関する次の属性を追跡することができます-
- タイトル
- 著者
- 件名
- ブックID
派生データ型の定義
派生データ type を定義するには、typeおよび end type ステートメントが使用されます。 。 typeステートメントは、プログラムに複数のメンバーを持つ新しいデータ型を定義します。 タイプステートメントの形式はこれです-
type type_name
declarations
end type
ここにあなたが本の構造を宣言する方法があります-
type Books
character(len = 50) :: title
character(len = 50) :: author
character(len = 150) :: subject
integer :: book_id
end type Books
構造体メンバーへのアクセス
派生データ型のオブジェクトは、構造体と呼ばれます。
タイプブックの構造は、次のようなタイプ宣言文で作成できます-
type(Books) :: book1
構造のコンポーネントは、コンポーネントセレクター文字(%)を使用してアクセスできます-
book1%title = "C Programming"
book1%author = "Nuha Ali"
book1%subject = "C Programming Tutorial"
book1%book_id = 6495407
- %記号の前後にスペースがないことに注意してください。 *
例
次のプログラムは、上記の概念を示しています-
program deriveDataType
!type declaration
type Books
character(len = 50) :: title
character(len = 50) :: author
character(len = 150) :: subject
integer :: book_id
end type Books
!declaring type variables
type(Books) :: book1
type(Books) :: book2
!accessing the components of the structure
book1%title = "C Programming"
book1%author = "Nuha Ali"
book1%subject = "C Programming Tutorial"
book1%book_id = 6495407
book2%title = "Telecom Billing"
book2%author = "Zara Ali"
book2%subject = "Telecom Billing Tutorial"
book2%book_id = 6495700
!display book info
Print* , book1%title
Print *, book1%author
Print *, book1%subject
Print *, book1%book_id
Print *, book2%title
Print *, book2%author
Print *, book2%subject
Print *, book2%book_id
end program deriveDataType
上記のコードをコンパイルして実行すると、次の結果が生成されます-
C Programming
Nuha Ali
C Programming Tutorial
6495407
Telecom Billing
Zara Ali
Telecom Billing Tutorial
6495700
構造の配列
また、派生型の配列を作成することができます-
type(Books), dimension(2) :: list
配列の個々の要素は次のようにアクセスできます-
list(1)%title = "C Programming"
list(1)%author = "Nuha Ali"
list(1)%subject = "C Programming Tutorial"
list(1)%book_id = 6495407
次のプログラムは、概念を示しています-
program deriveDataType
!type declaration
type Books
character(len = 50) :: title
character(len = 50) :: author
character(len = 150) :: subject
integer :: book_id
end type Books
!declaring array of books
type(Books), dimension(2) :: list
!accessing the components of the structure
list(1)%title = "C Programming"
list(1)%author = "Nuha Ali"
list(1)%subject = "C Programming Tutorial"
list(1)%book_id = 6495407
list(2)%title = "Telecom Billing"
list(2)%author = "Zara Ali"
list(2)%subject = "Telecom Billing Tutorial"
list(2)%book_id = 6495700
!display book info
Print *, list(1)%title
Print *, list(1)%author
Print *, list(1)%subject
Print *, list(1)%book_id
Print *, list(1)%title
Print *, list(2)%author
Print *, list(2)%subject
Print *, list(2)%book_id
end program deriveDataType
上記のコードをコンパイルして実行すると、次の結果が生成されます-
C Programming
Nuha Ali
C Programming Tutorial
6495407
C Programming
Zara Ali
Telecom Billing Tutorial
6495700
Fortran-ポインター
ほとんどのプログラミング言語では、ポインター変数はオブジェクトのメモリアドレスを格納します。 ただし、Fortranでは、ポインターはメモリアドレスを格納するだけでなく、より多くの機能を持つデータオブジェクトです。 タイプ、ランク、エクステント、メモリアドレスなど、特定のオブジェクトに関する詳細情報が含まれています。
ポインターは、割り当てまたはポインターの割り当てによってターゲットに関連付けられます。
ポインター変数の宣言
ポインター変数は、ポインター属性で宣言されます。
次の例は、ポインタ変数の宣言を示しています-
integer, pointer :: p1 ! pointer to integer
real, pointer, dimension (:) :: pra ! pointer to 1-dim real array
real, pointer, dimension (:,:) :: pra2 ! pointer to 2-dim real array
ポインタが指すことができます-
- 動的に割り当てられたメモリの領域。
- target 属性を持つ、ポインターと同じタイプのデータオブジェクト。
ポインターにスペースを割り当てる
*allocate* ステートメントを使用すると、ポインターオブジェクトにスペースを割り当てることができます。 たとえば-
program pointerExample
implicit none
integer, pointer :: p1
allocate(p1)
p1 = 1
Print *, p1
p1 = p1 + 4
Print *, p1
end program pointerExample
上記のコードをコンパイルして実行すると、次の結果が生成されます-
1
5
*deallocate* ステートメントが不要になったら、割り当てられたストレージスペースを空にし、未使用および使用不可のメモリスペースの蓄積を回避する必要があります。
ターゲットと関連付け
ターゲットは別の通常の変数で、スペースが確保されています。 ターゲット変数は target 属性で宣言する必要があります。
関連演算子(⇒)を使用して、ポインター変数をターゲット変数に関連付けます。
概念を実証するために、前の例を書き換えましょう-
program pointerExample
implicit none
integer, pointer :: p1
integer, target :: t1
p1=>t1
p1 = 1
Print *, p1
Print *, t1
p1 = p1 + 4
Print *, p1
Print *, t1
t1 = 8
Print *, p1
Print *, t1
end program pointerExample
上記のコードをコンパイルして実行すると、次の結果が生成されます-
1
1
5
5
8
8
ポインタは-
- 未定義
- 関連する
- 分離
上記のプログラムでは、⇒演算子を使用して、ポインターp1をターゲットt1に*関連付け*ています。 関連付けられた関数は、ポインターの関連付けステータスをテストします。
*nullify* ステートメントは、ポインターをターゲットから分離します。
同じターゲットを指すポインターが複数存在する可能性があるため、Nullifyはターゲットを空にしません。 ただし、ポインターを空にすると、無効化も暗示されます。
例1
次の例は、概念を示しています-
program pointerExample
implicit none
integer, pointer :: p1
integer, target :: t1
integer, target :: t2
p1=>t1
p1 = 1
Print *, p1
Print *, t1
p1 = p1 + 4
Print *, p1
Print *, t1
t1 = 8
Print *, p1
Print *, t1
nullify(p1)
Print *, t1
p1=>t2
Print *, associated(p1)
Print*, associated(p1, t1)
Print*, associated(p1, t2)
!what is the value of p1 at present
Print *, p1
Print *, t2
p1 = 10
Print *, p1
Print *, t2
end program pointerExample
上記のコードをコンパイルして実行すると、次の結果が生成されます-
1
1
5
5
8
8
8
T
F
T
0
0
10
10
コードを実行するたびに、メモリアドレスが異なることに注意してください。
例2
program pointerExample
implicit none
integer, pointer :: a, b
integer, target :: t
integer :: n
t = 1
a => t
t = 2
b => t
n = a + b
Print *, a, b, t, n
end program pointerExample
上記のコードをコンパイルして実行すると、次の結果が生成されます-
2 2 2 4
Fortran-基本入出力
これまでのところ、 read ステートメントを使用してキーボードからデータを読み取り、 *print ステートメントを使用して画面に出力を表示できることを確認しました。 この形式の入出力は、*自由形式 I/Oであり、*リスト指示*入出力と呼ばれます。
フリーフォーマットのシンプルなI/Oは次の形式を持っています-
read(*,*) item1, item2, item3...
print *, item1, item2, item3
write(*,*) item1, item2, item3...
ただし、フォーマットされたI/Oを使用すると、データ転送よりも柔軟性が高くなります。
書式付き入力出力
フォーマットされた入力出力は次のような構文を持っています-
read fmt, variable_list
print fmt, variable_list
write fmt, variable_list
どこで、
- fmtはフォーマット仕様です
- variable-listは、キーボードから読み取られるか、画面に書き込まれる変数のリストです
フォーマット仕様は、フォーマットされたデータの表示方法を定義します。 括弧で囲まれた*編集記述子*のリストを含む文字列で構成されます。
- 編集記述子*は、文字や数字が表示される正確な形式、たとえば幅、小数点以下の桁数などを指定します。
例えば
Print "(f6.3)", pi
次の表は、記述子を説明しています-
Descriptor | Description | Example |
---|---|---|
I | This is used for integer output. This takes the form ‘rIw.m’ where the meanings of r, w and m are given in the table below. Integer values are right justified in their fields. If the field width is not large enough to accommodate an integer then the field is filled with asterisks. | print "(3i5)", i, j, k |
F | This is used for real number output. This takes the form ‘rFw.d’ where the meanings of r, w and d are given in the table below. Real values are right justified in their fields. If the field width is not large enough to accommodate the real number then the field is filled with asterisks. | print "(f12.3)",pi |
E |
This is used for real output in exponential notation. The ‘E’ descriptor statement takes the form ‘rEw.d’ where the meanings of r, w and d are given in the table below. Real values are right justified in their fields. If the field width is not large enough to accommodate the real number then the field is filled with asterisks. 実数を小数点以下3桁で出力するには、少なくとも10のフィールド幅が必要であることに注意してください。 1つは仮数の符号、2つはゼロ、4つは仮数、2つは指数自体です。 一般的に、w≥d + 7。 |
print "(e10.3)",123456.0 gives ‘0.123e+06’ |
ES | This is used for real output (scientific notation). This takes the form ‘rESw.d’ where the meanings of r, w and d are given in the table below. The ‘E’ descriptor described above differs slightly from the traditional well known ‘scientific notation’. Scientific notation has the mantissa in the range 1.0 to 10.0 unlike the E descriptor which has the mantissa in the range 0.1 to 1.0. Real values are right justified in their fields. If the field width is not large enough to accommodate the real number then the field is filled with asterisks. Here also, the width field must satisfy the expressionw ≥ d + 7 | print "(es10.3)",123456.0 gives ‘1.235e+05’ |
A | This is used for character output. This takes the form ‘rAw’ where the meanings of r and w are given in the table below. Character types are right justified in their fields. If the field width is not large enough to accommodate the character string then the field is filled with the first ‘w’ characters of the string. | print "(a10)", str |
X | This is used for space output. This takes the form ‘nX’ where ‘n’ is the number of desired spaces. | print "(5x, a10)", str |
/ | Slash descriptor – used to insert blank lines. This takes the form ‘/’ and forces the next data output to be on a new line. | print "(/,5x, a10)", str |
次のシンボルは、フォーマット記述子で使用されます-
Sr.No | Symbol & Description |
---|---|
1 |
c 列番号 |
2 |
d 実際の入力または出力の小数点以下の桁数 |
3 |
m 表示される最小桁数 |
4 |
n スキップするスペースの数 |
5 |
r 繰り返し回数–記述子または記述子のグループを使用する回数 |
6 |
w フィールド幅-入力または出力に使用する文字数 |
例1
program printPi
pi = 3.141592653589793238
Print "(f6.3)", pi
Print "(f10.7)", pi
Print "(f20.15)", pi
Print "(e16.4)", pi/100
end program printPi
上記のコードをコンパイルして実行すると、次の結果が生成されます-
3.142
3.1415927
3.141592741012573
0.3142E-01
例2
program printName
implicit none
character (len = 15) :: first_name
print *,' Enter your first name.'
print *,' Up to 20 characters, please'
read *,first_name
print "(1x,a)",first_name
end program printName
上記のコードがコンパイルおよび実行されると、次の結果が生成されます(ユーザーがZaraという名前を入力すると仮定します)。
Enter your first name.
Up to 20 characters, please
Zara
実施例3
program formattedPrint
implicit none
real :: c = 1.2786456e-9, d = 0.1234567e3
integer :: n = 300789, k = 45, i = 2
character (len=15) :: str="Tutorials Point"
print "(i6)", k
print "(i6.3)", k
print "(3i10)", n, k, i
print "(i10,i3,i5)", n, k, i
print "(a15)",str
print "(f12.3)", d
print "(e12.4)", c
print '(/,3x,"n = ",i6, 3x, "d = ",f7.4)', n, d
end program formattedPrint
上記のコードをコンパイルして実行すると、次の結果が生成されます-
45
045
300789 45 2
300789 45 2
Tutorials Point
123.457
0.1279E-08
n = 300789 d = *******
フォーマットステートメント
formatステートメントを使用すると、1つのステートメントで文字、整数、および実際の出力を組み合わせて一致させることができます。 次の例はこれを示しています-
program productDetails
implicit none
character (len = 15) :: name
integer :: id
real :: weight
name = 'Ardupilot'
id = 1
weight = 0.08
print *,' The product details are'
print 100
100 format (7x,'Name:', 7x, 'Id:', 1x, 'Weight:')
print 200, name, id, weight
200 format(1x, a, 2x, i3, 2x, f5.2)
end program productDetails
上記のコードをコンパイルして実行すると、次の結果が生成されます-
The product details are
Name: Id: Weight:
Ardupilot 1 0.08
Fortran-ファイル入出力
Fortranでは、ファイルからデータを読み取り、ファイルにデータを書き込むことができます。
前の章では、端末からデータを読み取り、端末にデータを書き込む方法を説明しました。 この章では、Fortranが提供するファイルの入出力機能について学習します。
1つまたは複数のファイルを読み書きできます。 OPEN、WRITE、READ、およびCLOSEステートメントにより、これを実現できます。
ファイルの開閉
ファイルを使用する前に、ファイルを開く必要があります。 open コマンドは、読み取りまたは書き込みのためにファイルを開くために使用されます。 コマンドの最も単純な形式は-
open (unit = number, file = "name").
しかし、オープンステートメントは一般的な形式を持っている可能性があります-
open (list-of-specifiers)
次の表は、最も一般的に使用される指定子を説明しています-
Sr.No | Specifier & Description |
---|---|
1 |
[UNIT=] u ユニット番号は9〜99の範囲の任意の番号で、ファイルを示します。任意の番号を選択できますが、プログラム内のすべての開いているファイルには一意の番号が必要です。 |
2 |
IOSTAT= ios これはI/Oステータス識別子であり、整数変数でなければなりません。 openステートメントが成功した場合、返されるios値はゼロです。それ以外の場合はゼロ以外の値です。 |
3 |
ERR = err これは、エラーが発生した場合にコントロールがジャンプするラベルです。 |
4 |
FILE = fname ファイル名、文字列。 |
5 |
STATUS = sta ファイルの以前のステータスが表示されます。 文字列であり、3つの値NEW、OLD、またはSCRATCHのいずれかを持つことができます。 スクラッチファイルは、閉じられるかプログラムが終了すると作成および削除されます。 |
6 |
ACCESS = acc ファイルアクセスモードです。 SEQUENTIALまたはDIRECTの2つの値のいずれかを持つことができます。 デフォルトはSEQUENTIALです。 |
7 |
FORM = frm ファイルのフォーマット状態を示します。 FORMATTEDまたはUNFORMATTEDの2つの値のいずれかを持つことができます。 デフォルトはUNFORMATTEDです |
8 |
RECL = rl 直接アクセスファイルの各レコードの長さを指定します。 |
ファイルが開かれた後、読み取りおよび書き込みステートメントによってアクセスされます。 完了したら、 close ステートメントを使用して閉じます。
クローズステートメントには、次の構文があります-
close ([UNIT = ]u[,IOSTAT = ios,ERR = err,STATUS = sta])
括弧内のパラメーターはオプションであることに注意してください。
- 例 *
この例は、ファイルにデータを書き込むために新しいファイルを開くことを示しています。
program outputdata
implicit none
real, dimension(100) :: x, y
real, dimension(100) :: p, q
integer :: i
! data
do i=1,100
x(i) = i* 0.1
y(i) = sin(x(i)) * (1-cos(x(i)/3.0))
end do
! output data into a file
open(1, file = 'data1.dat', status = 'new')
do i=1,100
write(1,*) x(i), y(i)
end do
close(1)
end program outputdata
上記のコードがコンパイルおよび実行されると、ファイルdata1.datが作成され、xおよびy配列値がそこに書き込まれます。 そして、ファイルを閉じます。
ファイルの読み取りと書き込み
読み取りステートメントと書き込みステートメントは、それぞれファイルの読み取りと書き込みに使用されます。
彼らは次の構文を持っています-
read ([UNIT = ]u, [FMT = ]fmt, IOSTAT = ios, ERR = err, END = s)
write([UNIT = ]u, [FMT = ]fmt, IOSTAT = ios, ERR = err, END = s)
ほとんどの指定子は、上記の表ですでに説明されています。
END = s指定子は、ファイルの終わりに達したときにプログラムがジャンプするステートメントラベルです。
- 例 *
この例は、ファイルの読み取りと書き込みを示しています。
このプログラムでは、ファイルから読み取り、前の例で作成したdata1.datを作成し、画面に表示します。
program outputdata
implicit none
real, dimension(100) :: x, y
real, dimension(100) :: p, q
integer :: i
! data
do i = 1,100
x(i) = i* 0.1
y(i) = sin(x(i)) * (1-cos(x(i)/3.0))
end do
! output data into a file
open(1, file = 'data1.dat', status='new')
do i = 1,100
write(1,*) x(i), y(i)
end do
close(1)
! opening the file for reading
open (2, file = 'data1.dat', status = 'old')
do i = 1,100
read(2,*) p(i), q(i)
end do
close(2)
do i = 1,100
write(*,*) p(i), q(i)
end do
end program outputdata
上記のコードをコンパイルして実行すると、次の結果が生成されます-
0.100000001 5.54589933E-05
0.200000003 4.41325130E-04
0.300000012 1.47636665E-03
0.400000006 3.45637114E-03
0.500000000 6.64328877E-03
0.600000024 1.12552457E-02
0.699999988 1.74576249E-02
0.800000012 2.53552198E-02
0.900000036 3.49861123E-02
1.00000000 4.63171229E-02
1.10000002 5.92407547E-02
1.20000005 7.35742599E-02
1.30000007 8.90605897E-02
1.39999998 0.105371222
1.50000000 0.122110792
1.60000002 0.138823599
1.70000005 0.155002072
1.80000007 0.170096487
1.89999998 0.183526158
2.00000000 0.194692180
2.10000014 0.202990443
2.20000005 0.207826138
2.29999995 0.208628103
2.40000010 0.204863414
2.50000000 0.196052119
2.60000014 0.181780845
2.70000005 0.161716297
2.79999995 0.135617107
2.90000010 0.103344671
3.00000000 6.48725405E-02
3.10000014 2.02930309E-02
3.20000005 -3.01767997E-02
3.29999995 -8.61928314E-02
3.40000010 -0.147283033
3.50000000 -0.212848678
3.60000014 -0.282169819
3.70000005 -0.354410470
3.79999995 -0.428629100
3.90000010 -0.503789663
4.00000000 -0.578774154
4.09999990 -0.652400017
4.20000029 -0.723436713
4.30000019 -0.790623367
4.40000010 -0.852691114
4.50000000 -0.908382416
4.59999990 -0.956472993
4.70000029 -0.995793998
4.80000019 -1.02525222
4.90000010 -1.04385209
5.00000000 -1.05071592
5.09999990 -1.04510069
5.20000029 -1.02641726
5.30000019 -0.994243503
5.40000010 -0.948338211
5.50000000 -0.888650239
5.59999990 -0.815326691
5.70000029 -0.728716135
5.80000019 -0.629372001
5.90000010 -0.518047631
6.00000000 -0.395693362
6.09999990 -0.263447165
6.20000029 -0.122622721
6.30000019 2.53026206E-02
6.40000010 0.178709000
6.50000000 0.335851669
6.59999990 0.494883657
6.70000029 0.653881252
6.80000019 0.810866773
6.90000010 0.963840425
7.00000000 1.11080539
7.09999990 1.24979746
7.20000029 1.37891412
7.30000019 1.49633956
7.40000010 1.60037732
7.50000000 1.68947268
7.59999990 1.76223695
7.70000029 1.81747139
7.80000019 1.85418403
7.90000010 1.87160957
8.00000000 1.86922085
8.10000038 1.84674001
8.19999981 1.80414569
8.30000019 1.74167395
8.40000057 1.65982044
8.50000000 1.55933595
8.60000038 1.44121361
8.69999981 1.30668485
8.80000019 1.15719533
8.90000057 0.994394958
9.00000000 0.820112705
9.10000038 0.636327863
9.19999981 0.445154816
9.30000019 0.248800844
9.40000057 4.95488606E-02
9.50000000 -0.150278628
9.60000038 -0.348357052
9.69999981 -0.542378068
9.80000019 -0.730095863
9.90000057 -0.909344316
10.0000000 -1.07807255
Fortran-プロシージャ
*procedure* は、明確に定義されたタスクを実行し、プログラムから呼び出すことができるステートメントのグループです。 情報(またはデータ)は呼び出し側プログラムに渡され、引数としてプロシージャに渡されます。
手順の2種類があります-
- 関数
- サブルーチン
関数
関数は、単一の数量を返すプロシージャです。 関数は引数を変更しないでください。
返される数量は*関数値*と呼ばれ、関数名で示されます。
- 構文 *
関数の構文は次のとおりです-
function name(arg1, arg2, ....)
[declarations, including those for the arguments]
[executable statements]
end function [name]
次の例は、area_of_circleという名前の関数を示しています。 半径rの円の面積を計算します。
program calling_func
real :: a
a = area_of_circle(2.0)
Print* , "The area of a circle with radius 2.0 is"
Print *, a
end program calling_func
! this function computes the area of a circle with radius r
function area_of_circle (r)
! function result
implicit none
! dummy arguments
real :: area_of_circle
! local variables
real :: r
real :: pi
pi = 4 *atan (1.0)
area_of_circle = pi* r**2
end function area_of_circle
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
The area of a circle with radius 2.0 is
12.5663710
次のことに注意してください-
- メインプログラムとプロシージャの両方で implicit none を指定する必要があります。
- 呼び出された関数の引数rは、*ダミー引数*と呼ばれます。
結果オプション
返された値を関数名以外の名前で保存する場合は、 result オプションを使用できます。
次のように戻り変数名を指定できます-
function name(arg1, arg2, ....) result (return_var_name)
[declarations, including those for the arguments]
[executable statements]
end function [name]
サブルーチン
サブルーチンは値を返しませんが、引数を変更できます。
構文
subroutine name(arg1, arg2, ....)
[declarations, including those for the arguments]
[executable statements]
end subroutine [name]
サブルーチンを呼び出す
*call* ステートメントを使用してサブルーチンを呼び出す必要があります。
次の例は、引数の値を変更するサブルーチンスワップの定義と使用法を示しています。
program calling_func
implicit none
real :: a, b
a = 2.0
b = 3.0
Print *, "Before calling swap"
Print *, "a = ", a
Print *, "b = ", b
call swap(a, b)
Print *, "After calling swap"
Print *, "a = ", a
Print *, "b = ", b
end program calling_func
subroutine swap(x, y)
implicit none
real :: x, y, temp
temp = x
x = y
y = temp
end subroutine swap
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Before calling swap
a = 2.00000000
b = 3.00000000
After calling swap
a = 3.00000000
b = 2.00000000
引数の意図を指定する
intent属性を使用すると、プロシージャで引数を使用する意図を指定できます。 次の表は、意図属性の値を提供します-
Value | Used as | Explanation |
---|---|---|
in | intent(in) | Used as input values, not changed in the function |
out | intent(out) | Used as output value, they are overwritten |
inout | intent(inout) | Arguments are both used and overwritten |
次の例は、概念を示しています-
program calling_func
implicit none
real :: x, y, z, disc
x = 1.0
y = 5.0
z = 2.0
call intent_example(x, y, z, disc)
Print *, "The value of the discriminant is"
Print *, disc
end program calling_func
subroutine intent_example (a, b, c, d)
implicit none
! dummy arguments
real, intent (in) :: a
real, intent (in) :: b
real, intent (in) :: c
real, intent (out) :: d
d = b *b - 4.0* a * c
end subroutine intent_example
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
The value of the discriminant is
17.0000000
再帰的手続き
再帰は、プログラミング言語で同じ関数内の関数を呼び出すことができる場合に発生します。 関数の再帰呼び出しと呼ばれます。
プロシージャがそれ自体を直接または間接的に呼び出す場合、再帰的プロシージャと呼ばれます。 このタイプのプロシージャは、宣言の前に recursive という語を先行して宣言する必要があります。
関数が再帰的に使用される場合、 result オプションを使用する必要があります。
以下は、再帰的な手順を使用して、指定された数値の階乗を計算する例です-
program calling_func
implicit none
integer :: i, f
i = 15
Print *, "The value of factorial 15 is"
f = myfactorial(15)
Print *, f
end program calling_func
! computes the factorial of n (n!)
recursive function myfactorial (n) result (fac)
! function result
implicit none
! dummy arguments
integer :: fac
integer, intent (in) :: n
select case (n)
case (0:1)
fac = 1
case default
fac = n *myfactorial (n-1)
end select
end function myfactorial
内部手続き
プロシージャがプログラム内に含まれる場合、プログラムの内部プロシージャと呼ばれます。 内部手順を含むための構文は次のとおりです-
program program_name
implicit none
! type declaration statements
! executable statements
. . .
contains
! internal procedures
. . .
end program program_name
次の例は、概念を示しています-
program mainprog
implicit none
real :: a, b
a = 2.0
b = 3.0
Print* , "Before calling swap"
Print *, "a = ", a
Print *, "b = ", b
call swap(a, b)
Print *, "After calling swap"
Print *, "a = ", a
Print *, "b = ", b
contains
subroutine swap(x, y)
real :: x, y, temp
temp = x
x = y
y = temp
end subroutine swap
end program mainprog
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Before calling swap
a = 2.00000000
b = 3.00000000
After calling swap
a = 3.00000000
b = 2.00000000
Fortran-モジュール
モジュールは、非常に大きなプログラムを作成している場合、または関数またはサブルーチンを複数のプログラムで使用できる場合に、関数とサブルーチンを保持できるパッケージのようなものです。
モジュールは、プログラムを複数のファイルに分割する方法を提供します。
モジュールはのために使用されます-
- サブプログラム、データ、インターフェイスブロックのパッケージ化。
- 複数のルーチンで使用できるグローバルデータの定義。
- 選択したルーチン内で使用可能にできる変数の宣言。
- 使用するために別のプログラムまたはサブルーチンにモジュールを完全にインポートする。
モジュールの構文
モジュールは2つの部分で構成されています-
- ステートメント宣言の仕様部
- サブルーチンおよび関数定義の一部を含む
モジュールの一般的な形式は-
module name
[statement declarations]
[contains [subroutine and function definitions] ]
end module [name]
プログラムへのモジュールの使用
あなたはuse文によってプログラムまたはサブルーチンにモジュールを組み込むことができます-
use name
その点に注意してください
- 必要な数のモジュールを追加できます。各モジュールは個別のファイルに含まれ、個別にコンパイルされます。
- モジュールはさまざまなプログラムで使用できます。
- モジュールは、同じプログラムで何度も使用できます。
- モジュール仕様部で宣言された変数は、モジュールに対してグローバルです。
- モジュールで宣言された変数は、モジュールが使用されるプログラムまたはルーチンのグローバル変数になります。
- useステートメントは、メインプログラム、または特定のモジュールで宣言されたルーチンまたは変数を使用する他のサブルーチンまたはモジュールに表示できます。
例
次の例は、概念を示しています-
module constants
implicit none
real, parameter :: pi = 3.1415926536
real, parameter :: e = 2.7182818285
contains
subroutine show_consts()
print*, "Pi = ", pi
print*, "e = ", e
end subroutine show_consts
end module constants
program module_example
use constants
implicit none
real :: x, ePowerx, area, radius
x = 2.0
radius = 7.0
ePowerx = e * *x
area = pi* radius**2
call show_consts()
print*, "e raised to the power of 2.0 = ", ePowerx
print*, "Area of a circle with radius 7.0 = ", area
end program module_example
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Pi = 3.14159274
e = 2.71828175
e raised to the power of 2.0 = 7.38905573
Area of a circle with radius 7.0 = 153.938049
モジュール内の変数とサブルーチンのアクセシビリティ
デフォルトでは、モジュール内のすべての変数とサブルーチンは、 use ステートメントによって、モジュールコードを使用しているプログラムで使用可能になります。
ただし、 private および public 属性を使用して、モジュールコードのアクセシビリティを制御できます。 一部の変数またはサブルーチンをプライベートとして宣言すると、モジュール外では使用できません。
例
次の例は、概念を示しています-
前の例では、 e と* pi。*の2つのモジュール変数がありました。それらをプライベートにして、出力を観察しましょう-
module constants
implicit none
real, parameter,private :: pi = 3.1415926536
real, parameter, private :: e = 2.7182818285
contains
subroutine show_consts()
print*, "Pi = ", pi
print*, "e = ", e
end subroutine show_consts
end module constants
program module_example
use constants
implicit none
real :: x, ePowerx, area, radius
x = 2.0
radius = 7.0
ePowerx = e * *x
area = pi* radius**2
call show_consts()
print*, "e raised to the power of 2.0 = ", ePowerx
print*, "Area of a circle with radius 7.0 = ", area
end program module_example
上記のプログラムをコンパイルして実行すると、次のエラーメッセージが表示されます-
ePowerx = e * *x
1
Error: Symbol 'e' at (1) has no IMPLICIT type
main.f95:19.13:
area = pi* radius**2
1
Error: Symbol 'pi' at (1) has no IMPLICIT type
*e* と* pi、*は両方ともprivateと宣言されているため、プログラムmodule_exampleはこれらの変数にアクセスできなくなります。
ただし、他のモジュールサブルーチンはそれらにアクセスできます-
module constants
implicit none
real, parameter,private :: pi = 3.1415926536
real, parameter, private :: e = 2.7182818285
contains
subroutine show_consts()
print*, "Pi = ", pi
print*, "e = ", e
end subroutine show_consts
function ePowerx(x)result(ePx)
implicit none
real::x
real::ePx
ePx = e * *x
end function ePowerx
function areaCircle(r)result(a)
implicit none
real::r
real::a
a = pi* r**2
end function areaCircle
end module constants
program module_example
use constants
implicit none
call show_consts()
Print*, "e raised to the power of 2.0 = ", ePowerx(2.0)
print*, "Area of a circle with radius 7.0 = ", areaCircle(7.0)
end program module_example
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Pi = 3.14159274
e = 2.71828175
e raised to the power of 2.0 = 7.38905573
Area of a circle with radius 7.0 = 153.938049
Fortran-組み込み関数
組み込み関数は、Fortran言語の一部として提供される一般的で重要な関数です。 これらの関数のいくつかについては、配列、文字、文字列の章で既に説明しました。
組み込み関数は次のように分類できます-
- 数値関数
- 数学関数
- 数値照会関数
- 浮動小数点操作関数
- ビット操作関数
- キャラクター関数
- 種類の機能
- 論理関数
- 配列関数。
配列の章で配列関数について説明しました。 次のセクションでは、他のカテゴリのこれらすべての機能について簡単に説明します。
関数名の列で、
- Aは、あらゆるタイプの数値変数を表します
- Rは実数または整数変数を表します
- XとYは実変数を表します
- Zは複素変数を表します *Wは実変数または複素変数を表します
数値関数
Sr.No | Function & Description |
---|---|
1 |
Aの絶対値を返します |
2 |
AIMAG (Z) 複素数Zの虚数部を返します |
3 |
AINT (A [, KIND]) Aの小数部分をゼロに切り捨て、実数を返します。 |
4 |
ANINT (A [, KIND]) 実際の値、最も近い整数または整数を返します。 |
5 |
CEILING (A [, KIND]) 数値A以上の最小の整数を返します。 |
6 |
CMPLX (X [, Y, KIND]) 実変数XおよびYを複素数X + iYに変換します。 Yがない場合、0が使用されます。 |
7 |
CONJG (Z) 複素数Zの複素共役を返します。 |
8 |
DBLE (A) Aを倍精度実数に変換します。 |
9 |
DIM (X, Y) XとYの正の差を返します。 |
10 |
DPROD (X, Y) XとYの倍精度実積を返します。 |
11 |
FLOOR (A [, KIND]) 数値A以下の最大の整数を提供します。 |
12 |
INT (A [, KIND]) 数値(実数または整数)を整数に変換し、実数部をゼロに切り捨てます。 |
13 |
MAX (A1, A2 [, A3,…]) すべて同じ型の引数から最大値を返します。 |
14 |
MIN (A1, A2 [, A3,…]) すべて同じタイプの引数から最小値を返します。 |
15 |
MOD (A, P) Pによる除算でAの残りを返します。両方の引数は同じ型です(A-INT(A/P) *P) |
16 |
Pを法とするAを返します:(A-FLOOR(A/P) *P) |
17 |
数値Aの最も近い整数を返します |
18 |
REAL (A [, KIND]) 実数型に変換します |
19 |
SIGN (A, B) Aの絶対値にPの符号を掛けた値を返します。 基本的に、Bの符号をAに転送します。 |
例
program numericFunctions
implicit none
! define constants
! define variables
real :: a, b
complex :: z
! values for a, b
a = 15.2345
b = -20.7689
write(*,*) 'abs(a): ',abs(a),' abs(b): ',abs(b)
write(*,*) 'aint(a): ',aint(a),' aint(b): ',aint(b)
write(*,*) 'ceiling(a): ',ceiling(a),' ceiling(b): ',ceiling(b)
write(*,*) 'floor(a): ',floor(a),' floor(b): ',floor(b)
z = cmplx(a, b)
write(*,*) 'z: ',z
end program numericFunctions
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
abs(a): 15.2344999 abs(b): 20.7688999
aint(a): 15.0000000 aint(b): -20.0000000
ceiling(a): 16 ceiling(b): -20
floor(a): 15 floor(b): -21
z: (15.2344999, -20.7688999)
数学関数
Sr.No | Function & Description |
---|---|
1 |
ACOS (X) ラジアンで、範囲(0、π)の逆余弦を返します。 |
2 |
ASIN (X) ラジアン単位の範囲(-π/2、π/2)で逆正弦を返します。 |
3 |
ATAN (X) 範囲(-π/2、π/2)の逆正接をラジアンで返します。 |
4 |
ATAN2 (Y, X) 範囲(-π、π)の逆正接をラジアンで返します。 |
5 |
COS (X) 引数のコサインをラジアンで返します。 |
6 |
COSH (X) 引数の双曲線余弦をラジアンで返します。 |
7 |
EXP (X) Xの指数値を返します。 |
8 |
LOG (X) Xの自然対数値を返します。 |
9 |
LOG10 (X) Xの常用対数(底10)値を返します。 |
10 |
SIN (X) 引数のサインをラジアンで返します。 |
11 |
SINH (X) 引数の双曲線正弦をラジアンで返します。 |
12 |
SQRT (X) Xの平方根を返します。 |
13 |
TAN (X) 引数のタンジェントをラジアンで返します。 |
14 |
TANH (X) 引数の双曲線正接をラジアンで返します。 |
例
次のプログラムは、時間t後の発射体の水平および垂直位置xおよびyをそれぞれ計算します-
ここで、x = u t cos aおよびy = u t sin a-g t2/2
program projectileMotion
implicit none
! define constants
real, parameter :: g = 9.8
real, parameter :: pi = 3.1415927
!define variables
real :: a, t, u, x, y
!values for a, t, and u
a = 45.0
t = 20.0
u = 10.0
! convert angle to radians
a = a *pi/180.0
x = u* cos(a) *t
y = u* sin(a) *t - 0.5* g *t* t
write(*,*) 'x: ',x,' y: ',y
end program projectileMotion
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
x: 141.421356 y: -1818.57861
数値照会関数
これらの関数は、整数および浮動小数点演算の特定のモデルで機能します。 関数は、変数Xと同じ種類の数値のプロパティを返します。これは、実数である場合があり、整数になる場合があります。
Sr.No | Function & Description |
---|---|
1 |
DIGITS (X) モデルの有効桁数を返します。 |
2 |
EPSILON (X) 1に比べてほとんど無視できる数を返します。 つまり、REAL(1.0、KIND(X))+ EPSILON(X)がREAL(1.0、KIND(X))と等しくないような最小値を返します。 |
3 |
HUGE (X) モデルの最大数を返します |
4 |
MAXEXPONENT (X) モデルの最大指数を返します |
5 |
MINEXPONENT (X) モデルの最小指数を返します |
6 |
PRECISION (X) 小数精度を返します |
7 |
RADIX (X) モデルのベースを返します |
8 |
RANGE (X) 10進数の指数範囲を返します |
9 |
TINY (X) モデルの最小の正数を返します |
浮動小数点操作関数
Sr.No | Function & Description |
---|---|
1 |
EXPONENT (X) モデル番号の指数部を返します |
2 |
FRACTION (X) 数値の小数部分を返します |
3 |
NEAREST (X, S) 指定された方向に最も近い異なるプロセッサ番号を返します |
4 |
RRSPACING (X) 指定された数値の近くのモデル番号の相対間隔の逆数を返します |
5 |
SCALE (X, I) 実数とその底を整数の累乗で乗算します |
6 |
SET_EXPONENT (X, I) 数値の指数部を返します |
7 |
SPACING (X) 指定された数値の近くのモデル番号の絶対間隔を返します |
ビット操作関数
Sr.No | Function & Description |
---|---|
1 |
BIT_SIZE (I) モデルのビット数を返します |
2 |
BTEST (I, POS) ビットテスト |
3 |
IAND (I, J) 論理積 |
4 |
IBCLR (I, POS) クリアビット |
5 |
IBITS (I, POS, LEN) ビット抽出 |
6 |
IBSET (I, POS) ビットを設定 |
7 |
IEOR (I, J) 排他的OR |
8 |
IOR (I, J) 包括的OR |
9 |
ISHFT (I, SHIFT) 論理シフト |
10 |
ISHFTC (I, SHIFT [, SIZE]) 循環シフト |
11 |
NOT (I) 論理補数 |
キャラクター関数
Sr.No | Function & Description |
---|---|
1 |
ACHAR (I) ASCII照合シーケンスのI番目の文字を返します。 |
2 |
ADJUSTL (STRING) 先頭の空白を削除し、末尾の空白を挿入することにより、文字列を調整します |
3 |
ADJUSTR (STRING) 末尾の空白を削除して先頭の空白を挿入することにより、文字列を調整します。 |
4 |
CHAR (I [, KIND]) マシン固有の照合シーケンスでI番目の文字を返します |
5 |
IACHAR © ASCII照合シーケンスでの文字の位置を返します。 |
6 |
ICHAR © マシン(プロセッサ)固有の照合シーケンスでの文字の位置を返します。 |
7 |
INDEX (STRING, SUBSTRING [, BACK]) STRING内のSUBSTRINGの左端(BACKが.TRUEの場合は右端)の開始位置を返します。 |
8 |
LEN (STRING) 文字列の長さを返します。 |
9 |
LEN_TRIM (STRING) 末尾の空白文字なしで文字列の長さを返します。 |
10 |
LGE (STRING_A, STRING_B) 字句的に大きいか等しい |
11 |
LGT (STRING_A, STRING_B) 字句的により大きい |
12 |
LLE (STRING_A, STRING_B) 字句的に小さいか等しい |
13 |
LLT (STRING_A, STRING_B) 字句的により小さい |
14 |
REPEAT (STRING, NCOPIES) 繰り返し連結 |
15 |
SCAN (STRING, SET [, BACK]) SETに属するSTRINGの左端(BACKが.TRUE。の場合は右端)の文字のインデックスを返します。SETに属する文字がない場合は0を返します。 |
16 |
TRIM (STRING) 末尾の空白文字を削除します |
17 |
VERIFY (STRING, SET [, BACK]) 文字列内の文字セットを検証します |
種類の機能
Sr.No | Function & Description |
---|---|
1 |
KIND (X) kind型パラメーター値を返します。 |
2 |
SELECTED_INT_KIND ® 指定された指数範囲の種類パラメータの種類を返します。 |
3 |
SELECTED_REAL_KIND ([P, R]) 与えられた精度と範囲の実種類のパラメーター値 |
論理関数
Sr.No | Function & Description |
---|---|
1 |
LOGICAL (L [, KIND]) 異なるkind型パラメーターを持つ論理型のオブジェクト間の変換 |
Fortran-数値精度
Fortranの古いバージョンでは、デフォルトの実数型と*倍精度*型の2つの*実数*型があることをすでに説明しました。
ただし、Fortran 90/95では、 kind 指定により、実データ型および整数データ型の精度をより詳細に制御できます。
種類の属性
異なる種類の番号は、コンピューター内で異なる方法で保存されます。 kind 属性を使用すると、数値を内部的に保存する方法を指定できます。 例えば、
real, kind = 2 :: a, b, c
real, kind = 4 :: e, f, g
integer, kind = 2 :: i, j, k
integer, kind = 3 :: l, m, n
上記の宣言では、実変数e、f、およびgは、実変数a、b、およびcよりも精度が高くなっています。 整数変数l、m、およびnは、整数変数i、j、およびkよりも大きな値を格納でき、格納する桁数が多くなります。 これはマシンに依存しますが。
例
program kindSpecifier
implicit none
real(kind = 4) :: a, b, c
real(kind = 8) :: e, f, g
integer(kind = 2) :: i, j, k
integer(kind = 4) :: l, m, n
integer :: kind_a, kind_i, kind_e, kind_l
kind_a = kind(a)
kind_i = kind(i)
kind_e = kind(e)
kind_l = kind(l)
print *,'default kind for real is', kind_a
print *,'default kind for int is', kind_i
print *,'extended kind for real is', kind_e
print *,'default kind for int is', kind_l
end program kindSpecifier
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
default kind for real is 4
default kind for int is 2
extended kind for real is 8
default kind for int is 4
変数のサイズを問い合わせる
数値のサイズを調べることができる組み込み関数がいくつかあります。
たとえば、* bit_size(i)組み込み関数は、ストレージに使用されるビット数を指定します。 実数の場合、 precision(x)組み込み関数は精度の10進数の桁数を返し、 range(x)*組み込み関数は指数の10進数の範囲を返します。
例
program getSize
implicit none
real (kind = 4) :: a
real (kind = 8) :: b
integer (kind = 2) :: i
integer (kind = 4) :: j
print *,'precision of real(4) =', precision(a)
print *,'precision of real(8) =', precision(b)
print *,'range of real(4) =', range(a)
print *,'range of real(8) =', range(b)
print *,'maximum exponent of real(4) =' , maxexponent(a)
print *,'maximum exponent of real(8) =' , maxexponent(b)
print *,'minimum exponent of real(4) =' , minexponent(a)
print *,'minimum exponent of real(8) =' , minexponent(b)
print *,'bits in integer(2) =' , bit_size(i)
print *,'bits in integer(4) =' , bit_size(j)
end program getSize
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
precision of real(4) = 6
precision of real(8) = 15
range of real(4) = 37
range of real(8) = 307
maximum exponent of real(4) = 128
maximum exponent of real(8) = 1024
minimum exponent of real(4) = -125
minimum exponent of real(8) = -1021
bits in integer(2) = 16
bits in integer(4) = 32
種類の値を取得する
Fortranは、整数と実数の必要な精度の種類の値を取得するために、さらに2つの組み込み関数を提供します-
- selected_int_kind(r) *selected_real_kind([p、r])
selected_real_kind関数は、指定された10進精度pおよび10進指数範囲rに必要なkind型パラメーター値である整数を返します。 10進数の精度は有効桁数であり、10進数の指数範囲は表現可能な最小および最大の数値を指定します。 したがって、範囲は10-r〜10 + rです。
たとえば、selected_real_kind(p = 10、r = 99)は、小数点以下10桁の精度と少なくとも10-99から10 + 99の範囲に必要な種類の値を返します。
例
program getKind
implicit none
integer:: i
i = selected_real_kind (p = 10, r = 99)
print* ,'selected_real_kind (p = 10, r = 99)', i
end program getKind
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
selected_real_kind (p = 10, r = 99) 8
Fortran-プログラムライブラリ
さまざまなFortranツールとライブラリがあります。 一部は無料で、一部は有料サービスです。
以下はいくつかの無料のライブラリです-
- RANDLIB、乱数および統計分布ジェネレーター
- BLAS
- EISPACK
- GAMS–NIST利用可能な数学ソフトウェアガイド
- NISTの統計的およびその他のルーチン
- ラパック
- LINPACK
- MINPACK
- マッドパック
- NCAR数学ライブラリ
- 数学的ソフトウェア、論文、およびデータベースのNetlibコレクション。
- ODEPACK
- ODERPACK、ランキングと順序付けのための一連のルーチン。
- 行列指数を計算するためのExpokit
- スレート
- SPECFUN
- STARPAC
- StatLib統計ライブラリ
- TOMS
- 文字列のソートとマージ
次のライブラリは無料ではありません-
- NAG Fortran数値ライブラリー
- Visual Numerics IMSLライブラリ
- 数値レシピ
Fortran-プログラミングスタイル
プログラミングスタイルとは、プログラムの開発中にいくつかのルールに従うことです。 これらの優れた実践により、プログラムに読みやすさや曖昧さなどの価値が与えられます。
良いプログラムは、次の特性を持つ必要があります-
- 読みやすさ
- 適切な論理構造
- わかりやすいメモとコメント
たとえば、次のようなコメントを作成する場合、それはあまり役に立ちません-
! loop from 1 to 10
do i = 1,10
ただし、二項係数を計算していて、nCrにこのループが必要な場合は、このようなコメントが役立ちます-
! loop to calculate nCr
do i = 1,10
- コードブロックをインデントして、さまざまなレベルのコードを明確にします。
- ゼロによる除算、負の実数の平方根、負の実数の対数などの数値エラーがないことを確認するための自己チェックコード。
- 変数が不正な値や範囲外の値をとらないようにするコード、つまり入力検証を含める。
- 不要な場所にチェックを入れずに実行を遅くします。 たとえば-
real :: x
x = sin(y) + 1.0
if (x >= 0.0) then
z = sqrt(x)
end if
- 適切なアルゴリズムを使用して明確に記述されたコード。
- 継続マーカー「&」を使用して長い表現を分割します。
- 意味のある変数名を作成します。
Fortran-デバッグプログラム
デバッガーツールを使用して、プログラムのエラーを検索します。
デバッガープログラムはコードをステップ実行し、プログラムの実行中に変数やその他のデータオブジェクトの値を調べることができます。
ソースコードが読み込まれ、デバッガ内でプログラムを実行することになります。 デバッガは次の方法でプログラムをデバッグします-
- ブレークポイントの設定、
- ソースコードをステップスルーし、
- 監視ポイントを設定します。
ブレークポイントは、特に重要なコード行の後、プログラムが停止する場所を指定します。 変数がブレークポイントでチェックされた後のプログラム実行。
デバッガープログラムは、ソースコードも1行ずつチェックします。
監視ポイントは、特に読み取りまたは書き込み操作の後、いくつかの変数の値を確認する必要があるポイントです。
gdbデバッガー
gdbデバッガー、GNUデバッガーはLinuxオペレーティングシステムに付属しています。 X Windowsシステムの場合、gdbにはグラフィカルインターフェイスが付属しており、プログラムの名前はxxgdbです。
次の表に、gdbのいくつかのコマンドを示します-
Command | Purpose |
---|---|
break | Setting a breakpoint |
run | Starts execution |
cont | Continues execution |
next | Executes only the next line of source code, without stepping into any function call |
step | Execute the next line of source code by stepping into a function in case of a function call. |
dbxデバッガー
Linux用の別のデバッガー、dbxデバッガーがあります。
次の表は、dbxのいくつかのコマンドを示しています-
Command | Purpose |
---|---|
stop[var] | Sets a breakpoint when the value of variable var changes. |
stop in [proc] | It stops execution when a procedure proc is entered |
stop at [line] | It sets a breakpoint at a specified line. |
run | Starts execution. |
cont | Continues execution. |
next | Executes only the next line of source code, without stepping into any function call. |
step | Execute the next line of source code by stepping into a function in case of a function call. |