Converting working code from double precision to four-precision: how to read four-point numbers in FORTRAN from the input file

I have a big, old, FORTRAN 77 code that worked for many, many years without any problems. Double precision is no longer enough, so for converting to quadruple precision I have:

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

The program compiles without errors or warnings using the gcc-4.6 compiler on

  • operating system
  • : openSUSE 11.3 x86_64 (64-bit operating system)
  • 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 the input file with numbers in it. These numbers were in the form 1.234D + 02 (for the double-precision version of the code that works). I changed them so that the number is 1.234Q + 02, however I get a runtime error:

Bad real number in list entry 1

Indicating that a routine that reads data from an input file (called read.f) does not find the first number in the input file to be compatible with the expected one.

It is strange that the version with four precision does not complain when the input file contains numbers, such as 1.234D + 02 or 123.4 (which, based on the output, seems to be automatically converted to form 1.234D + 02, rather than Q + 02), I just don’t like Q + 02, so it seems that gcc-4.6 does not allow reading four-point numbers from input files in scientific notation!

Has anyone ever read four times the exact number in scientific notation (for example, 1234Q + 02) from FORTRAN using a gcc compiler, and if so, how did you get it? (or do you need another compiler / operating system / hardware to make it work?)

+4
source share
1 answer

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

I suggest mixing a bit of Fortran 90 into your FORTRAN 77 code.

Write all your numbers with an "E". Change your other program to write data this way. Do not worry about "D" and do not try to use the rarely supported "Q". (Using "Q" in constants in the source code is an extension of gfortran - see 6.1.8 in the manual.)

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

use ISO_FORTRAN_ENV WP = real128 

or

 use ISO_FORTRAN_ENV WP = real64 

like a variation that changes whether your code uses double or quadruple precision. This uses the Fortran ISO environment to select types according to their number of bits. ( use should be between program and implicit none , assignment statement after implicit none .)

Then declare your real variables with:

 real (WP) :: MyVar 

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

WP == Working accuracy.

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

Why not write a tiny test program to try?

+4
source

Source: https://habr.com/ru/post/1496055/


All Articles