问题描述
我有一个非常大的代码,它设置并迭代求解一个非线性偏微分方程系统,用 fortran 编写.我需要所有变量都是双精度的.在我为代码编写的附加模块中,我将所有变量声明为双精度类型,但我的模块仍然使用旧源代码中声明为实数类型的变量.所以我的问题是,当单精度变量乘以 fortran 中的双精度变量时会发生什么?如果用于存储值的变量声明为双精度,结果是否为双精度?如果双精度值乘以最后没有D0"的常数怎么办?我可以在 Intel 11.1 中设置一个编译器选项来使所有实数/双精度/双精度常量吗?
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?
推荐答案
所以我的问题是,fortran 中单精度变量乘以双精度变量会发生什么?被提升为双精度,运算以双精度完成.
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.
如果用于存储值的变量声明为双精度,结果是否为双精度?不一定.右侧是一个不知道"左侧变量精度的表达式,它将被存储在其中.如果你有 Double = SingleA * SingleB (使用名称表示类型),计算将以单精度执行,然后转换为双精度存储.这不会获得额外的计算精度!
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!
如果一个双精度值乘以一个末尾没有D0"的常数呢?这就像第一个问题一样,常数会提升为双精度,然后计算以双精度完成.然而,这个常数仍然是单精度的,即使你写了很多数字作为双精度常数,内部存储是单精度的,不能代表那个精度.例如,DoubleVar * 3.14159265359 将以双精度计算,但将以双精度计算 DoubleVar * 3.14159.
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.
如果你想让编译器在一个常量中保留许多数字,你必须指定一个常量的精度.Fortran 90 的方法是使用您需要的任何精度定义您自己的真实类型,例如,要求至少 14 个十进制数字:
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 中获得双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!