Fortran 95 中的数值精度: [英] Numerical Precision in Fortran 95:
问题描述
我有以下 Fortran 代码:
奇怪的程序真实(种类=8)::Pi1=3.1415926535897932384626433832795028841971693993751058209;真实(种类=8)::Pi2=3.1415926535897932384626433832795028841971693993751058209_8;打印*, "Pi1=", Pi1;打印*, "Pi2=", Pi2;结束程序奇怪
我用gfortran编译,输出是:
Pi1= 3.1415927410125732Pi2= 3.1415926535897931
当然第二个是正确的,但应该是这样吗?似乎 Pi1 作为单精度数字输入到内存,然后放入双精度内存插槽.但这对我来说似乎是一个错误.我说的对吗?
我确实懂一点Fortran!@Dougal 的答案是正确的,尽管他引用的片段不是,将字母 d
嵌入到真正的文字常量中不是必需的(从 Fortran 90 开始),确实许多 Fortran 程序员现在认为这种方法是过时的.该片段在建议使用 3.1415926535d+0
为 pi 初始化 64 位浮点值时也具有误导性,它没有将足够多的数字设置为正确的值.p>
声明:
Real(Kind=8)::Pi1=3.1415926535897932384626433832795028841971693993751058209
将 Pi1
定义为类型 8 的实数变量.然而,文字实数值 3.1415926535897932384626433832795028841971693993751058209
是默认类型的实数值,最有可能是 4-byte real 在大多数当前编译器上.这似乎可以解释您的输出,但请检查您的文档.
另一方面,文字实值 Pi2=3.1415926535897932384626433832795028841971693993751058209_8
是,通过种类规范的后缀,声明为 kind=8,这与变量的种类相同它被分配到.
还有三点:
1) 不要误以为 kind=8
与 64 位浮点数
或 double<意思相同/代码>.对于许多编译器来说,它确实如此,而对于某些编译器却没有.种类编号在 Fortran 实现之间不可移植.根据标准,它们是任意正整数.更好的是,使用现代编译器,使用内部模块
iso_fortran_env
中的预定义常量,例如
使用,内在 :: iso_fortran_env...真实(真实64) :: pi = 3.14159265358979323846264338_real64
还有其他可移植的方法来使用诸如 selected_real_kind
之类的函数来设置变量种类.
2) 由于 pi
的值在程序执行期间不太可能发生变化,因此您可能需要将其设为参数:
real(real64), 参数:: pi = 3.14159265358979323846264338_real64
3) 没有必要(或通常)以;"结束 Fortran 语句除非您想在源文件的同一行中包含多个语句.
I have the following Fortran code:
Program Strange
Real(Kind=8)::Pi1=3.1415926535897932384626433832795028841971693993751058209;
Real(Kind=8)::Pi2=3.1415926535897932384626433832795028841971693993751058209_8;
Print*, "Pi1=", Pi1;
Print*, "Pi2=", Pi2;
End Program Strange
I compile with gfortran, and the output is:
Pi1= 3.1415927410125732
Pi2= 3.1415926535897931
Of course the second is correct, but should this be the case? It seems like Pi1 is being input to memory as a single precision number, and then put into a double precision memory slot. But this seems like an error to me. Am I correct?
I do know a bit of Fortran ! @Dougal's answer is correct though the snippet he quotes from is not, embedding the letter d
into a real literal constant is not required (since Fortran 90), indeed many Fortran programmers now regard that approach as archaic. The snippet is also misleading in advising the use of 3.1415926535d+0
to initialise a 64-bit floating-point value for pi, it doesn't set enough of the digits to their correct values.
The statement:
Real(Kind=8)::Pi1=3.1415926535897932384626433832795028841971693993751058209
defines Pi1
to be a real variable of kind 8. The literal real value 3.1415926535897932384626433832795028841971693993751058209
is, however, a real value of default kind, most likely to be a 4-byte real on most current compilers. That seems to explain your output but do check your documentation.
On the other hand, the literal real value Pi2=3.1415926535897932384626433832795028841971693993751058209_8
is, by the suffixing of the kind specification, declared to be of kind=8 which is the same as the kind of the variable it is assigned to.
Three more points:
1) Don't fall into the trap of thinking that kind=8
means the same thing as 64-bit floating-point number
or double
. For many compilers it does, for some it doesn't. Kind numbers are not portable between Fortran implementations. They are, according to the standard, arbitrary positive integers. Better, with a modern compiler, would be to use the predefined constants from the intrinsic module iso_fortran_env
, e.g.
use, intrinsic :: iso_fortran_env
...
real(real64) :: pi = 3.14159265358979323846264338_real64
There are other portable approaches to setting variable kinds using functions such as selected_real_kind
.
2) Since the value of pi
is unlikely to change during the execution of your program you might care to make it a parameter thus:
real(real64), parameter :: pi = 3.14159265358979323846264338_real64
3) It isn't necessary (or usual) to end Fortran statements with a ';' unless you want to have more than one statement on the same line in the source file.
这篇关于Fortran 95 中的数值精度:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!