0

I have the following code in FORTRAN 77:

REAL*8 :: dm

dm=1.-1.E-12

write(6,*) 'dm: ', dm

I get: dm: 1

Is this OK? I would like to get dm=0.999999999999

jpcgandre
  • 1,487
  • 5
  • 31
  • 55
  • I don't know enough about FORTRAN to answer authoritatively, but assuming that REAL*8 represents an 8 byte float (double precision) it should have at least 15 digits of precision. Could it have something to do with the output formatting? – axblount Jun 07 '13 at 20:15
  • 3
    The trouble is your constants are single precision, try 1.D0-1.D-12. Note that notation (along with REAL*8) is obsolete in modern fortran but still works. Not clear if/why you want f77.. – agentp Jun 07 '13 at 20:21

1 Answers1

3

As stated in a comment, you need to specify the precision of the constants. Also, real*8 is obsolete. (Was it always an extension?) Here is a modern way to write this, using the ISO Fortran Environment to obtain a 64-bit real type and using that type both in the declaration and in the constants.

use ISO_FORTRAN_ENV

real (real64) :: dm  
dm = 1.0_real64 - 1.0E-12_real64

For more info, see What does `real*8` mean?

Community
  • 1
  • 1
M. S. B.
  • 28,968
  • 2
  • 46
  • 73