Real precision in Fortran Wiki
In [[Fortran 90]] and later, intrinsic types such as real and integer have a kind attribute which guarantees a specific precision and/or range. `real*8` and counterparts should no longer be used (Chin, Worth, and Greenough, 2006, p. 5). `double precision` is also no longer needed and can be thought of as a `kind` of `real`. The following example declares two `real` variables `a` and `b` which are guaranteed to have at least 15 significant digits of precision and an exponent range of at least 307 (Lemmon and Schafer, 2005, p. 20-22): integer, parameter :: dp = selected_real_kind(15, 307) real(kind=dp) :: a real(dp) :: b {: lang=fortran } One can also guarantee precision up to that of the machine-compiler-specific double precision `real` by using the [[kind]] function to get the `kind` of `1.d0`: integer, parameter :: dp = kind(1.d0) {: lang=fortran } Another possibility, suggested by Metcalf, et. al (2004, p. 71), ensures that the double and quad types are actually twice and four times the precision of a single: integer, parameter :: & sp = kind(1.0), & dp = selected_real_kind(2*precision(1.0_sp)), & qp = selected_real_kind(2*precision(1.0_dp)) {: lang=fortran } Finally, precision of 32-, 64-, and 128-bit reals can usually be obtained with the following constants: integer, parameter :: sp = selected_real_kind(6, 37) integer, parameter :: dp = selected_real_kind(15, 307) integer, parameter :: qp = selected_real_kind(33, 4931) {: lang=fortran } In Fortran 2008, also the following constants are available: use, intrinsic :: iso_fortran_env integer, parameter :: sp = REAL32 integer, parameter :: dp = REAL64 integer, parameter :: qp = REAL128 {: lang=fortran } ## External Links * [how to declare doubles in f95](http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/932cb33652397d9e/) on [[comp lang fortran|comp.lang.fortran]] (28 Jan 2009). * [example of KIND to replace DOUBLE PRECISION ](http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/cb6d2ad659d7ce3b) on [[comp lang fortran|comp.lang.fortran]] (8 May 2009). ## References * Chin, L. S., D. J. Worth and C. Greenough (2006). [Thoughts on using the Features of Fortran 95](http://www.softeng.rl.ac.uk/st/archive/SoftEng/SESP/Publications/FortranDosDont/FortranDosDont.pdf). Software Engineering Group Notes no. SEG-N-003. * Metcalf, M., J. Reid, and M. Cohen (2004). [Fortran 95/2003 Explained](http://www.amazon.com/exec/obidos/ASIN/0198526938/fortran-wiki-20). Oxford University Press. * Lemmon, D. R. and J. L. Schafer. (2005). [Developing Statistical Software in Fortran 95](http://www.amazon.com/exec/obidos/ASIN/0387238174/fortran-wiki-20). Springer.
AltStyle
によって変換されたページ
(->オリジナル)
/
アドレス:
モード:
デフォルト
音声ブラウザ
ルビ付き
配色反転
文字拡大
モバイル