将工作代码从双精度转换为四精度:如何从输入文件读取FORTRAN中的四倍精度数字 [英] Converting a working code from double-precision to quadruple-precision: How to read quadruple-precision numbers in FORTRAN from an input file

查看:1015
本文介绍了将工作代码从双精度转换为四精度:如何从输入文件读取FORTRAN中的四倍精度数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的,旧的FORTRAN 77代码,已经工作了很多年,没有问题。
双精度是不够的,所以要转换为四倍精度我有:


  1. 更换所有REAL * 8到REAL * 16

  2. 将所有函数(如DCOS())替换为像COS()这样的函数

  3. 替换所有内置数字,如0.d0到0.q0和1D + 01到1Q + 01

该程序编译时没有错误或警告,使用gcc-4.6编译器on


  1. 操作系统:openSUSE 11.3 x86_64(64位操作系统)
  2. 硬件:英特尔至强E5-2650(Sandy Bridge)

我的LD_LIBRARY_PATH变量设置为64位库文件夹:
/ gcc-4.6 / lib64



程序读取一个包含数字的输入文件。
这些数字的格式为1.234D + 02(代码的双精度版本)。
我改变了它们,所以现在这个数字是1.234Q + 02,但是我得到了运行时错误:

列表输入

指示从输入文件读取数据的子例程(称为 read.f )未找到这是输入文件中的第一个数字,与预期的兼容性。



奇怪的是,代码的四倍精度版本没有 抱怨当输入文件包含数字如1.234D + 02或123.4(根据输出似乎自动转换为格式1.234D + 02而不是Q + 02)时,它只是不喜欢Q + 02,所以看来gcc-4.6不允许用科学记数法从输入文件读入四位数的精确数字!



有没有人能够读取一个输入文件在科学记数法(即像1234Q + 02)在一个GCC编译器的FORTRAN中的四倍精度数字,如果是的话,你有它的工作? (或者你是否需要一个不同的编译器/操作系统/硬件才能使它工作?)

几乎所有这些已经在@IanH和@Vladimi的评论中。



我建议在FORTRAN 77代码中混合一点Fortran 90。



用E写出所有的数字。改变你的其他程序以这种方式写入数据。不要打扰D,也不要尝试使用不常使用的Q。 (在源代码中常量中使用Q是gfortran的扩展 - 见手册6.1.8)

由于您希望相同的源代码支持两个精度在程序的顶部有:

 使用ISO_FORTRAN_ENV 
WP = real128

code>

 使用ISO_FORTRAN_ENV 
WP = real64

作为您的代码使用双精度或四倍精度。这是使用ISO Fortran Environment根据其位数选择类型。 (使用需要在程序隐式无之间);在 implicit none 之后的赋值语句。)然后通过以下方式声明您的实际变量:

 真实(WP):: MyVar 



<在源代码中,将实常量写为 1.23456789012345E + 12_WP _type 是指定常量类型的Fortran 90方法。这样,您只需更改定义 WP



WP的单行即可在双精度和四倍精度之间来回切换。 ==工作精度。



只需在输入文件中使用E即可。 Fortran将根据变量的类型读取数据。



为什么不写一个小测试程序来试试呢?


I have a big, old, FORTRAN 77 code that has worked for many, many years with no problems. Double-precision is not enough anymore, so to convert to quadruple-precision I have:

  1. Replaced all occurrences of REAL*8 to REAL*16
  2. Replaced all functions like DCOS() into functions like COS()
  3. Replaced all built-in numbers like 0.d0 to 0.q0 , and 1D+01 to 1Q+01

The program compiles with no errors or warnings with the gcc-4.6 compiler on

  1. operating system: openSUSE 11.3 x86_64 (a 64-bit operating system)
  2. hardware: Intel Xeon E5-2650 (Sandy Bridge)

My LD_LIBRARY_PATH variable is set to the 64-bit library folder: /gcc-4.6/lib64

The program reads an input file that has numbers in it. Those numbers used to be of the form 1.234D+02 (for the double-precision version of the code, which works). I have changed them so now that number is 1.234Q+02 , however I get the runtime error:

Bad real number in item 1 of list input

Indicating that the subroutine that reads in the data from the input file (called read.f) does not find the first number in the inputfile, to be compatible with what it expected.

Strangely, the quadruple-precision version of the code does not complain when the input file contains numbers like 1.234D+02 or 123.4 (which, based on the output seems to automatically be converted to the form 1.234D+02 rather than Q+02), it just does not like the Q+02 , so it seems that gcc-4.6 does not allow quadruple-precision numbers to be read in from input files in scientific notation !

Has anyone ever been able to read from an input file a quadruple-precision number in scientific notation (ie, like 1234Q+02) in FORTRAN with a gcc compiler, and if so what how did you get it to work ? (or did you need a different compiler/operating system/hardware to get it to work ?)

解决方案

Almost all of this is already in comments by @IanH and @Vladimi.

I suggest mixing in a little Fortran 90 into your FORTRAN 77 code.

Write all of your numbers with "E". Change your other program to write the data this way. Don't bother with "D" and don't try to use the infrequently supported "Q". (Using "Q" in constants in source code is an extension of gfortran -- see 6.1.8 in manual.)

Since you want the same source code to support two precisions, at the top of the program, have:

use ISO_FORTRAN_ENV
WP = real128

or

use ISO_FORTRAN_ENV
WP = real64

as the variation that changes whether your code is using double or quadruple precision. This is using the ISO Fortran Environment to select the types by their number of bits. (use needs to between program and implicit none; the assignment statement after implicit none.)

Then declare your real variables via:

real (WP) :: MyVar

In source code, write real constants as 1.23456789012345E+12_WP. The _type is the Fortran 90 way of specifying the type of a constant. This way you can go back and forth between double and quadruple precision by only changing the single line defining WP

WP == Working Precision.

Just use "E" in input files. Fortran will read according to the type of the variable.

Why not write a tiny test program to try it out?

这篇关于将工作代码从双精度转换为四精度:如何从输入文件读取FORTRAN中的四倍精度数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆