
- Fortran 教程
- Fortran - 首頁
- Fortran - 概述
- Fortran - 環境設定
- Fortran - 基本語法
- Fortran - 資料型別
- Fortran - 變數
- Fortran - 常量
- Fortran - 運算子
- Fortran - 決策
- Fortran - 迴圈
- Fortran - 數字
- Fortran - 字元
- Fortran - 字串
- Fortran - 陣列
- Fortran - 動態陣列
- Fortran - 派生資料型別
- Fortran - 指標
- Fortran - 基本輸入輸出
- Fortran - 檔案輸入輸出
- Fortran - 過程
- Fortran - 模組
- Fortran - 本質函式
- Fortran - 數值精度
- Fortran - 程式庫
- Fortran - 程式設計風格
- Fortran - 程式除錯
- Fortran 資源
- Fortran - 快速指南
- Fortran - 有用資源
- Fortran - 討論
Fortran - 數值精度
我們已經討論過,在較舊版本的 Fortran 中,存在兩種實數型別:預設實數型別和雙精度型別。
但是,Fortran 90/95 透過kind說明符提供了對實數和整數資料型別精度的更多控制。
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) 內在函式返回精度的小數位數,而range(x) 內在函式返回指數的小數範圍。
示例
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
獲取 Kind 值
Fortran 提供了另外兩個內在函式來獲取所需精度整數和實數的 kind 值:
- selected_int_kind (r)
- selected_real_kind ([p, r])
selected_real_kind 函式返回一個整數,該整數是給定十進位制精度 p 和十進位制指數範圍 r 所需的 kind 型別引數值。十進位制精度是有效數字的位數,十進位制指數範圍指定可表示的最小和最大數字。因此,範圍是從 10-r 到 10+r。
例如,selected_real_kind (p = 10, r = 99) 返回實現 10 位小數精度和至少 10-99 到 10+99 範圍所需的 kind 值。
示例
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
廣告