* ******************************************************************************* * * This program illustrates the use of the TEMPREAL data type for * intermediate results of double-precision arithmetic. While TEMPREAL * precision is default for intermediate results within expressions, * the TEMPREAL data type allows treatment of larger blocks of statements * (such as loops, or even whole subprograms) as "expressions", under * programmer control. * * The operator is prompted for two real values between 0.0 and 4.0, * a starting value and a multiplicative factor, which are used to fill a * 500-element array with double-precision values and compute an exact * double-precision sum. The percentage of each element is then computed, * and totalled. The result, which should be 100%, is printed. * * For comparison purposes, the identical computation is done using a * double-precision intermediate variable, and the amount of error in- * troduced by the loss of precision is printed. * ******************************************************************************* * PROGRAM PROG2 DOUBLE PRECISION RARRAY,RTOTAL,RESULT,DPRES TEMPREAL TMPRES COMMON RTOTAL, RARRAY(500) CALL GETDAT DPRES = 0.0 TMPRES = 0.0 DO 10, I = 1,500 DPRES = DPRES + RARRAY(I)/RTOTAL TMPRES = TMPRES + RARRAY(I)/RTOTAL 10 CONTINUE RESULT = TMPRES PRINT 100, RESULT, DPRES 100 FORMAT ('RESULT = ', E26.20E2, ', D-P RESULT = ', E26.20E2) RESULT = DPRES - RESULT PRINT 200, RESULT 200 FORMAT ('DIFFERENCE = ', E13.5E4) END SUBROUTINE GETDAT DOUBLE PRECISION RARRAY,RTOTAL,RVALUE,FACTOR TEMPREAL TMPTOT COMMON RTOTAL, RARRAY(500) TMPTOT = 0.0 PRINT 100 100 FORMAT('ENTER STARTING VALUE BETWEEN 0.00 AND 4.00 IN F4.2 FORMAT') READ 200, RVALUE 200 FORMAT(F4.2) PRINT 300 300 FORMAT('ENTER MULTIPLICATIVE FACTOR BETWEEN 0.00 AND 4.00 IN F4.2 FORMAT') READ 200, FACTOR DO 10, I = 1, 500 RARRAY(I) = RVALUE TMPTOT = TMPTOT + RVALUE RVALUE = RVALUE * FACTOR 10 CONTINUE RTOTAL = TMPTOT END