Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REAL*4 REAL*8 CONFLICT

Status
Not open for further replies.

nnarry

Programmer
Sep 3, 2007
1
CH
I am working with an old fortran program.
My problem is a conflict between variables of REAL*4 and REAL*8 type.
The dummy argument used in the declaration of a subroutine in the program is of the REAL*8 type.
The actual argument used for calling this subroutine is of the REAL*4 type.
When the subroutine is called and executed and control returns back to the main program, the values of the dummy and actual argument turn out to be different.
To illustrate this with a simple program, I wrote a subroutine that assigns values from 2,4,6,8,10,12 to an array. The array is the argument for the subroutine; it is of type REAL*8 as the dummy argument (CP) inside the subroutine and of type REAL*4 while calling this subroutine as the actual argument (AP).

The dummy argument values computed inside the subroutine are:
IN SUBROUTINE REAL*8 CP( 1 )= 2.00000000000000
IN SUBROUTINE REAL*8 CP( 2 )= 4.00000000000000
IN SUBROUTINE REAL*8 CP( 3 )= 6.00000000000000
IN SUBROUTINE REAL*8 CP( 4 )= 8.00000000000000
IN SUBROUTINE REAL*8 CP( 5 )= 10.0000000000000
IN SUBROUTINE REAL*8 CP( 6 )= 12.0000000000000

However while returning back to the main program, the actual argument values are:
AFTER RETURNING FROM SUBROUTINE REAL*4AP( 1 )= 0.0000000E+00
AFTER RETURNING FROM SUBROUTINE REAL*4AP( 2 )= 2.000000
AFTER RETURNING FROM SUBROUTINE REAL*4AP( 3 )= 0.0000000E+00
AFTER RETURNING FROM SUBROUTINE REAL*4AP( 4 )= 2.250000
AFTER RETURNING FROM SUBROUTINE REAL*4AP( 5 )= 0.0000000E+00
AFTER RETURNING FROM SUBROUTINE REAL*4AP( 6 )= 2.375000

Could anyone please explain why the two different sets of values are obtained?
 
The parameters are doubling up. ie 2 of your real*4 are being used for one real*8.
 
A REAL*4 variable occupies 4 bytes. A REAL*8 8 bytes.

On some systems, e.g. Gould-SEL MPX and DEC VAX/VMS the two number systems are interchangeable - a REAL*4 and a REAL*8 at the same address have the same value except that the REAL*8 has greater precision. With IEEE numbers the values are different.

If a sub-program call writes a REAL*8 to a REAL*4 location, the next 4 bytes will be overwritten and the result will be data corruption or a stack dump. If a sub-program call reads a REAL*4 to a REAL*8 location no error occurs under VAX/VMS or MPX, but the wrong value is read with IEEE numbers, for example, on a PC.

Some compilers, e.g. FTN95 ( and some qa and re-engineering tools, e.g. WinFPT ( or PlusFort ( trap and report, and automatically correct this problem.

Best wishes,

John.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top