fortran中的参数精度 [英] Parameter precision in fortran

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

问题描述

我正在使用intel fortran2016.我定义了一些精度变量,如下所示:

I am using intel fortran 2016. I have defined some precision variables as follows:

 ! definition of single, double and quad precision
        integer, parameter :: SINGLE_PRECISION = selected_real_kind(6, 37)
        integer, parameter :: DOUBLE_PRECISION = selected_real_kind(15, 307) ! is used as standard precision
        integer, parameter :: QUAD_PRECISION = selected_real_kind(33, 4931)

        ! definition of variable precision for REAL, INTEGER
        integer, parameter :: REAL_TYPE = DOUBLE_PRECISION
        integer, parameter :: INTEGER_TYPE = 4

我现在想使用它们来控制在子例程中声明的参数的精度,如下所示:

I would like to now use these to control the precision of a parameter that gets declared in a subroutine as follows:

  SUBROUTINE SKIP(IUNIT,NBYTS)

  IMPLICIT DOUBLE PRECISION (A-H,O-Z)
  Character c*1
  Parameter(n1 = 1024, nT1 = 8*n1)

我尝试了以下方法:

Parameter(INTEGER_TYPE)((n1 = 1024, nT1 = 8*n1)
Parameter(INTEGER_TYPE)((n1 = 1024, nT1 = 8*n1, kind = INTEGER_TYPE)

全部无济于事.在Fortran中定义参数精度的正确方法是什么?

All to no avail. What is the proper way to define parameter precision in Fortran?

谢谢

推荐答案

注意:这与

Note: this is essentially the same as Francescalus' answer, but with some extra fluf.

此处出现的问题与 IMPLICIT 语句有关.

The issue at hand here is related to the IMPLICIT statement.

Fortran标准发表以下声明:

5.5 IMPLICIT语句

  1. 在范围界定单元中, IMPLICIT 语句指定类型,并且对于所有隐式类型的数据实体,其类型可能为参数名称以语句中指定的字母之一开头.或者,它可能表明没有隐式键入规则要应用于特定的范围界定单元.

  1. In a scoping unit, an IMPLICIT statement specifies a type, and possibly type parameters, for all implicitly typed data entities whose names begin with one of the letters specified in the statement. Alternatively, it may indicate that no implicit typing rules are to apply in a particular scoping unit.

< snip>

在每个作用域中,之间存在一个映射,该映射可能为空每个字母 A,B,...,Z 和一个类型(以及类型参数). IMPLICIT 语句指定其中字母的映射信件规格清单. IMPLICIT NONE 指定所有的空映射这些信.如果未为字母指定映射,则程序单元或接口主体的默认值为默认整数,如果该字母为 I,J ,...或 N ,否则为默认实数,并且内部或模块过程的默认值是主机中的映射作用域.

In each scoping unit, there is a mapping, which may be null, between each of the letters A, B, ..., Z and a type (and type parameters). An IMPLICIT statement specifies the mapping for the letters in its letter-spec-list. IMPLICIT NONE specifies the null mapping for all the letters. If a mapping is not specified for a letter, the default for a program unit or an interface body is default integer if the letter is I, J, ..., or N and default real otherwise, and the default for an internal or module procedure is the mapping in the host scoping unit.

因此,简而言之,默认情况下,所有内容的类型均为默认 REAL ,除非它以 I J ,..., N ,则其类型默认为 INTEGER .

So in short, by default everything is of type default REAL unless it starts with I,J,...,N, then it is of type default INTEGER.

在问题的示例中,除非另有说明,否则变量 n1 nT1 是默认的 INTEGER .因此,以下可能是一种解决方案:

In the example in the question, the variables n1 and nT1 are hence default INTEGER unless specified otherwise. Thus the following might be a solution :

subroutine skip(IUNIT,NBYTS)
   implicit double precision (A-H,O-Z)
   character c*1
   integer(kind=integer_kind), parameter :: n1 = 1024, nT1 = 8*n1
   ...
end subroutine skip

作为变量声明的一般说明,我想作以下说明:

As a general remark on variable declaration I would like to make the following remarks:

  • 默认使用不隐式.它使调试更加容易
  • 避免使用星号表示法,除字符外,它都不是标准的一部分,对于字符,它被声明为过时的.
  • 使用在固有模块 iso_fortran_env
  • 中声明的种类参数
  • 请注意,双精度不一定在字面意义或IEEE含义上表示双精度".这只是意味着,将 real 的存储量提高了两倍,并具有比 real 更高的精度.(精度可能是一位数字).
  • use implicit none as default. It makes debugging easier
  • avoid star notation, it is not part of the standard for anything but characters, and for characters it is declared obsolescent.
  • make use of kind parameters declared in the intrinsic module iso_fortran_env
  • be aware that double precision does not necessarily mean "double precision" in the literal or IEEE sense of the word. It just means, twice the storage of a real and a higher precision than real. (a higher precision could be one-digit).

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

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