实变量精度 [英] precision of real variable

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

问题描述

我在 FORTRAN 77 中有以下代码:

REAL*8 :: dmdm=1.-1.E-12写(6,*)'dm:',dm

我得到:dm: 1

这样好吗?我想获得 dm=0.999999999999

解决方案

如注释中所述,您需要指定常量的精度.此外,real*8 已过时.(它总是扩展吗?)这是一种现代的编写方式,使用 ISO Fortran 环境获取 64 位实数类型,并在声明和常量中使用该类型.

使用 ISO_FORTRAN_ENV真实的(real64)::dmdm = 1.0_real64 - 1.0E-12_real64

有关更多信息,请参阅 `real*8` 是什么意思?p>

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

解决方案

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?

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

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