Fortran 精确处理大实数 [英] Fortran handling large real numbers with precision

查看:21
本文介绍了Fortran 精确处理大实数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面的代码时,我得到的输出是 6378136.5 而不是 6378136.3

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

我已阅读此其他链接(Fortran 中实数的精度问题) 但它没有解释如何解决这个问题.

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

尽管由于浮点精度,这仍然会给您一个 6378136.2999999998 的值.

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

这篇关于Fortran 精确处理大实数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆