在现代Fortran中是否有必要将_kind附加到数字文字上? [英] Is it necessary to append _kind to number literals in modern Fortran?

查看:54
本文介绍了在现代Fortran中是否有必要将_kind附加到数字文字上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是在最近的一些测试之后,我有点困惑.我一直认为Fortran处理实数的方式如下(现代声明):

This might be stupid question but I'm a bit confused after some recent tests. I always thought the way Fortran deals with reals is the following (modern declaration):

program testReal

  implicit none

  integer, parameter :: rkind=8

  real(kind=rkind) :: a,b,c

  // Test 1
  a = 1
  b = 1.
  c = 1._rkind
  write(*,"(2(1xES23.15))") a, b, c

  // Test 2
  a = 1/2
  b = 1/2.
  c = 1./2.
  write(*,"(2(1xES23.15))") a, b, c

  // Test 3
  a = 3.3
  b = 1 * a
  c = 1. * a
  write(*,"(2(1xES23.15))") a, b, c

end program testReal

除了 Test 2 -a 以外,其他所有内容均相同.我一直以为我必须放 1._rkind,0.5_rkind等,以确保用零填充尾数的其余部分吗?

Apart from Test 2 - a everything evaulates the same. I always thought I have to put e.g. 1._rkind, 0.5_rkind, etc. after every real in order to make sure to fill the rest of the mantissa with zeros?

这是纯粹的运气还是真的不再需要附加 _rkind ?

Is this just pure luck or is it really not neccessary anymore to attach the _rkind?

推荐答案

首先让我们看一下测试1.这里是 1 1. 1._rkind是文字常量. 1 是默认种类的整数; 1.是默认类型的实数; 1._rkind 是实类型的 rkind (可能与默认类型相同).这些都是不同的东西.

Let's look first at Test 1. Here 1, 1. and 1._rkind are literal constants. 1 is an integer of default kind; 1. is a real of default kind; 1._rkind is a real of kind rkind (which could be the same kind as the default). These are all different things.

但是,在这种情况下,分配发生的事情是关键.由于 a b c 都是 rkind 类型的实数,因此相应的右侧都被转换为实数类型 rkind (假设这种类型的精度比默认类型大).转换等同于

However, in this case what happens on the assignment is key. As a, b and c are all reals of kind rkind the corresponding right-hand sides are all converted to a real of kind rkind (assuming such a kind has greater precision than default kind). The conversion is equivalent to

a = REAL(1, rkind)
b = REAL(1., rkind)
c = 1._rkind

恰好在您的数字模型中, 1 1.都可以完全转换为 1._rkind .

It just so happens that 1 and 1. are both, in your numeric model, convertable exactly to 1._rkind.

我不会涉及测试2,因为差异是明显的".

I won't touch on Test 2, as the differences are "obvious".

在测试3中,我们有文字常量 3.3 ,它是默认类型的实数.再次

In Test 3, we have the literal constant 3.3 which is a real of default kind. Again

a = REAL(3.3, rkind)
b = REAL(1, rkind)*REAL(3.3, rkind)
c = REAL(1., rkind)*REAL(3.3, rkind)

由于发生转换的位置和方式.从中您可以看到结果在一定程度上是相同的,并且算术运算与实实在在的 rkind 类似.

due to where and how conversions happen. From this you can see that the results are reasonably the same and the arithmetic happens as real of kind rkind.

您会发现与众不同

a = 3.3
b = 3.3_rkind

因为(数学上的)实数3.3在数字模型中无法精确表示.近似值会随默认种类和种类 rkind 的实数而不同.

because the (mathematical) real number 3.3 is not exactly representable in your numeric model. And the approximations will differ with real of default kind and kind rkind.

尤其是,无需担心用零填充尾数的其余部分".

In particular, there is no need to worry about "fill[ing] the rest of the mantissa with zeros".

这篇关于在现代Fortran中是否有必要将_kind附加到数字文字上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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