gfortran REAL不精确到小数点后8位 [英] gfortran REAL not accurate to 8 decimal places

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

问题描述

这个问题以前没有得到回答.我试图在Fortran中正确表示一个实数或任何数字. gfortran为我做的事情还遥遥无期.例如,当我声明变量REAL pi = 3.14159 fortran时,输出pi = 3.14159012而不是说3.14159000.见下文:

This question has not been previously answered. I am trying to represent a real or any number for that matter in Fortran correctly. What gfortran is doing for me is way off. For example when I declare the variable REAL pi=3.14159 fortran prints pi = 3.14159012 rather than say 3.14159000. See below:

PROGRAM Test
IMPLICIT NONE
REAL:: pi = 3.14159
PRINT *, "PI = ",pi
END PROGRAM Test

此打印:

PI = 3.14159012

我可能期望像PI = 3.14159000之类的东西,因为REAL应该精确到至少8位小数位.

I might have expected something like PI = 3.14159000 as a REAL is supposed to be accurate to at least 8 decimal places.

推荐答案

我心情很好,所以我将尝试回答这个问题,这是可以轻松搜索的基本知识(如已经在对这个问题和您以前的问题的评论).

I'm in a good mood, so I'll try to answer this question, which is basic knowledge which can be easily googled (as already pointed out in the comments to this and your former question).

幸运的是,Fortran提供了一些非常有趣的内在函数,以使您对浮点数有所了解.

Luckily, Fortran provides some really interesting intrinsics to get some understanding of floating point numbers.

您所说的8位数字是一个经验法则,可以与功能EPSILON(x)相关,该功能显示与1的最小偏差,该偏差可以在所选模型中表示(例如REAL4).该值实际上是1.19e-7,这意味着您的第8位数字很可能是错误的.我写的可能性最大,因为有些数字可以准确表示.

The 8 digits, you are talking about, are a rule of thumb and can be related to the function EPSILON(x), which prints the smallest deviation from 1, which can be represented within the chosen model (e.g. REAL4). This value is actually 1.19e-7 which means, that your 8th digit is most likely wrong. I write most likely, because some numbers can be represented exactly.

PI的情况下,可以使用固有的SPACING(PI)打印最小的可表示偏差.该值显示为2.38e-7,比epsilon稍大,但仍允许7个正确的数字.

In the case of PI, the smallest representable deviation can be printed using the intrinsic SPACING(PI). This shows a value of 2.38e-7, which is slightly larger than the epsilon and still allows for 7 correct digits.

现在,为什么您的PI值存储为3.14159012?存储浮点数时,始终存储最接近的可表示数字. 使用间距值,我们可以获得您的pi的可能值.可能的数字及其与您的3.14159值的差异:

Now, why does your value of PI get stored as 3.14159012? When you store a floating point number, you always store the nearest representable number. Using the value of spacing, we can get the possible values for your pi. Possible numbers and their differences to your value of 3.14159 are:

3.14158988         1.20E-007
3.14159012        -1.18E-007
3.14159036        -3.56E-007

如您所见,3.14159012是与3.14159最接近的可能值,因此被存储和打印.

As you can see, 3.14159012 is the nearest possible value to 3.14159 and is thus stored and printed.

这篇关于gfortran REAL不精确到小数点后8位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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