是否可以在 Fortran “data” 语句中自动提供双精度常量



>我有一个包含双精度实数值的数组xsolar

real*8        xsolar(5)

然后使用 data 语句用数字填充数组:

data xsolar/
     . 12.00, 10.93, 1.05, 1.38, 2.70/

默认情况下,看起来数字只设置最高有效字节,而其余字节包含随机数:

12.000000000000000
10.930000305175781
1.0499999523162842
1.3799999952316284
2.7000000476837158

我可以通过在数字末尾写d0来解决此问题:

data xsolar/
     * 12.00d0, 10.93d0, 1.05d0, 1.38d0, 2.70d0/

这将导致更好的精度:

12.000000000000000
10.930000000000000
1.0500000000000000
1.3799999999999999
2.7000000000000002

我的问题是我有一个巨大的数字列表,而不仅仅是五个。所以我必须在所有这些中添加d0。是否可以自动完成,因此此数据语句中的所有常量都被视为以 d0 结尾?更好的是,可以对我的所有数据语句或所有常量real*8完成吗?

我正在使用带有标志的 gfortran 6.3.0 进行编译:-O4 -ffixed-line-length-72 -ff2c .

一般来说,Fortran 确实区分了单精度和双精度文本,因此 10.9310.93d0 表示完全不同的对象(例如,请参阅此相关文章中(。

我的建议是研究启用某些(可能是非标准(转换的gfortran选项。 根据手册:

-fdefault-real-8
           Set the default real type to an 8 byte wide type.  Do nothing if
           this is already the default.  This option also affects the kind of
           non-double real constants like 1.0, and does promote the default
           width of "DOUBLE PRECISION" to 16 bytes if possible, unless
           "-fdefault-double-8" is given, too.
-fdefault-double-8
           Set the "DOUBLE PRECISION" type to an 8 byte wide type.  If
           -fdefault-real-8 is given, "DOUBLE PRECISION" would instead be
           promoted to 16 bytes if possible, and -fdefault-double-8 can be
           used to prevent this.  The kind of real constants like "1.d0" will
           not be changed by -fdefault-real-8 though, so also
           -fdefault-double-8 does not affect it.

因此,您似乎确实可以同时使用这两个选项,-fdefault-real-8 -fdefault-double-8,以使编译器将1.0这样的文字视为 64 位浮点数。 但是你必须确保这真的是你想要的,并且副作用不会影响代码的其余部分。

但是,由于您说您有一个很大的数字列表,并且您不太可能将它们作为文字嵌入到 data 语句的代码中,我认为将它们从文件中read到已经适当声明的数组变量中就足够了。

相关内容

最新更新