在 Fortran 中混淆双精度实数 [英] Confusing double precision real in Fortran
问题描述
我现在有一个紧迫的问题:在现代 Fortran 中声明双精度实数的公认"方式是什么?按照从最旧到最新的顺序,故事似乎是这样的:DOUBLE PRECISION
,然后是 REAL(kind=8)
,然后是 INTEGER, PARAMETER :: dp=kind(1.d0)
和 REAL(kind=dp)
--Metcalf 现在说 dp=kind(0.d0)
--现在 float32=selected_real_kind(6,37)
或 float64=selected_real_kind(15,307)
.所以...
Have this burning question on my mind right now: What is the "accepted" way to declare double precision real in modern Fortran? In order from oldest to newest, the story seems to go like this: DOUBLE PRECISION
, then REAL(kind=8)
, then INTEGER, PARAMETER :: dp=kind(1.d0)
with REAL(kind=dp)
--Metcalf now says dp=kind(0.d0)
--and now float32=selected_real_kind(6,37)
or float64=selected_real_kind(15,307)
. So...
- 我现在应该如何声明双精度真实?
- 在
REAL(kind=dp)
中kind
是多余的吗? - 在编译时是否需要任何特殊标志来使用
gfortran
或ifort
调用双精度实数?
- How should I be declaring double precision real now?
- Is
kind
redundant inREAL(kind=dp)
? - Are there any special flags needed at compile time to invoke double precision real with
gfortran
orifort
?
推荐答案
我个人现在写
use, intrinsic :: iso_fortran_env
其中包含int32
、real64
等参数,含义很明显,可以这样使用:
which includes parameters such as int32
,real64
which have the obvious meanings, and can be used like this:
real(real64) :: a_64_bit_real_scalar
请注意,标准不保证 kind=8
提供 8 字节的种类.kind 参数采用的值不是标准化的,并且因编译器而异.
Note that kind=8
is not guaranteed, by the standard, to deliver an 8-byte kind. The values that kind parameters take are not standardised and do vary from compiler to compiler.
如果需要,您可以编写诸如
You could, if you want, write statements such as
use, intrinsic :: iso_fortran_env, dp=>real64
...
real(dp) :: a_64_bit_real_scalar
这篇关于在 Fortran 中混淆双精度实数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!