使用intel 11.1编译器在fortran 90中获得双重精度 [英] Getting double precision in fortran 90 using intel 11.1 compiler
问题描述
所以我的问题是,当单精度变量乘以fortran中的双精度变量时会发生什么?单精度是提升到双精度,操作以双精度完成。
如果将用于存储值的变量声明为双精度,结果是否为双精度?不一定。右侧是一个表达式,它不会知道左侧的变量的精度,并将其存储起来。如果您有Double = SingleA * SingleB(使用名称来表示类型),则计算将以单精度执行,然后转换为双倍存储。这不会增加计算的精度!
如果双精度值乘以一个常数而没有D0结尾呢? / em>这就像第一个问题,常数将被提升到双精度,计算完成双精度。 然而,,常数仍然是单精度,即使你写了一个双精度常数的数字,内部存储也是单精度的,不能表示精度。例如,DoubleVar * 3.14159265359将以双精度计算,但会以双精度完成DoubleVar * 3.14159近似的值。
如果要使编译器在常量中保留许多位数,则必须具体指定常量的精度。 Fortran 90这样做的方法是用你需要的精确度定义你自己的真实类型,例如要求至少十四位十进制数字:
integer,parameter :: DoubleReal_K = selected_real_kind(14)
real(DoubleReal_K):: A
A = 5.0_DoubleReal_K
A = A * 3.14159265359_DoubleReal_K
I have a very large code that sets up and iteratively solves a system of non-linear partial differential equation, written in fortran. I need all variables to be double precision. In the additional module that I have written for the code, I declare all variables as the double precision type, but my module still uses variables from the old source code that are declared as type real. So my question is, what happens when a single-precision variable is multiplied by a double precision variable in fortran? Is the result double precision if the variable used to store the value is declared as double precision? And what if a double precision value is multiplied by a constant without the "D0" at the end? Can I just set a compiler option in Intel 11.1 to make all real/double precision/constants of double precision?
So my question is, what happens when a single-precision variable is multiplied by a double precision variable in fortran? The single precision is promote to double precision and the operation is done in double precision.
Is the result double precision if the variable used to store the value is declared as double precision? Not necessarily. The right-hand side is an expression that doesn't "know" about the precision of the variable on the left hand side, in to which it will be stored. If you have Double = SingleA * SingleB (using names to indicate the types), the calculation will be performed in single precision, then converted to double for storage. This will NOT gain extra precision for the calculation!
And what if a double precision value is multiplied by a constant without the "D0" at the end? This is just like the first question, the constant will be promoted to double precision and the calculation done in double precision. However, the constant is still single precision and even if you wrote down many digits as for a double-precision constant, the internal storage is single precision and cannot represent that accuracy. For example, DoubleVar * 3.14159265359 will be calculated in double precision, but will be something approximating DoubleVar * 3.14159 done in double precision.
If you want to have the compiler retain many digits in a constant, you must specific the precision of a constant. The Fortran 90 way to do this is to define your own real type with whatever precision that you need, e.g., to require at least 14 decimal digits:
integer, parameter :: DoubleReal_K = selected_real_kind (14)
real (DoubleReal_K) :: A
A = 5.0_DoubleReal_K
A = A * 3.14159265359_DoubleReal_K
这篇关于使用intel 11.1编译器在fortran 90中获得双重精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!