precision of real variable

2020-05-09 04:36发布

问题:

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

回答1:

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?