问题描述
当我运行下面的代码时,得到的结果是6378136 .5 而不是6378136 .3
PROGRAM测试
隐含无
真实* 8半径
半径= 6378136.3
print *,radius
END
我已经读过这个链接(),但它没有解释如何解决这个问题。
发生这种情况的原因并不是因为您使用的变量缺少精度,而是因为您已初始化价值使用一个单一的精确数字。
看看为一个很好的解释,并为任何大型程序的问题提供了一个优雅的解决方案。
如果你只是想要要快速解决它,那么你只需要改变一行:
radius = 6378136.3d0
虽然由于浮点精度的原因,这仍然会给你一个 6378136.2999999998
的值。
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.
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.
这篇关于Fortran精确处理大型实数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!