是否可以在 Fortran `data` 语句中自动提供双精度常量?
Is it possible to automatically supply double precision constants in Fortran `data` statement?
我有一个包含双精度实数值的数组 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.93
和 10.93d0
代表完全不同的对象(参见,例如,在 this related post 中)。
我的建议是查看启用某些(可能 non-standard)转换的 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
他们从一个文件到一个已经适当声明的数组变量。
我有一个包含双精度实数值的数组 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.93
和 10.93d0
代表完全不同的对象(参见,例如,在 this related post 中)。
我的建议是查看启用某些(可能 non-standard)转换的 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
他们从一个文件到一个已经适当声明的数组变量。