precision of real variable

折月煮酒 提交于 2019-12-31 07:17:08

问题


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?



来源:https://stackoverflow.com/questions/16992131/precision-of-real-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!