Fortran handling large real numbers with precision [duplicate]

試著忘記壹切 提交于 2019-12-12 04:34:54

问题


When I run the code below I get an output of 6378136.5 instead of 6378136.3

PROGRAM test

implicit none
real*8 radius

radius = 6378136.3

print*,radius

END

I have read this other link (Precision problems of real numbers in Fortran) but it doesn't explain how to fix this problem.


回答1:


The reason this is happening is not because the variable you are using lacks precision, but because you initialized the value using a single precision number.

Take a look at this answer for a good explanation, and an elegant solution to your problem for any larger programs.

If you just want to solve it quickly, then you only have to change one line:

radius = 6378136.3d0

Though this will still give you a value of 6378136.2999999998 because of floating point precision.



来源:https://stackoverflow.com/questions/42618704/fortran-handling-large-real-numbers-with-precision

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