我如何声明一个数字的精度是一个可调参数? [英] How do I declare the precision of a number to be an adjustable parameter?

查看:312
本文介绍了我如何声明一个数字的精度是一个可调参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2013年,有一个关于将大工作代码从双精度转换为四倍精度的问题:将工作代码从双精度转换为四倍精度:如何从输入文件读取FORTRAN中的四倍精度数字,以及一致意见是使用指定工作精度的可调整参数WP来声明变量,而不是使用D + 01声明变量的程序的单独版本,以及使用Q + 01的另一个版本。通过这种方式,我们可以通过在顶部定义WP = real128或WP = real64来轻松切换,其余部分不需要更改。

但是怎么做我们这样做?



我通过制作一个简单的代码TEST.F90来试着回答这个问题的建议:

 程序测试
使用ISO_FORTRAN_ENV
WP = real128
IMPLICIT NONE
real(WP):: X
X = 5.4857990945E-4_WP
END PROGRAM TEST

编译时间:

 〜/ gcc-4.6 / bin / gfortran -o tst.x TEST.F90 

但它给出:

  IMPLICIT NONE 
1
错误:(1)
处出现意外的IMPLICIT NONE语句QLEVEL16.F90:5.12:

real(WP):: MEL
1
错误:参数(1)处的'wp'尚未被声明或是一个变量,它不会减少为常量表达式
QLEVEL16.F90:6.29:

MEL = 5.4857990945E-4_WP
1
错误:缺少(1)
类型参数


解决方案

类型说明符必须是整数参数 - 并且不要适当地声明它。此外,隐式无必须在任何声明之前进行。



这是一个解决这两个问题的工作版本:

 程序测试
使用ISO_FORTRAN_ENV
IMPLICIT NONE
整数,参数:: WP = real128
real(WP):: X

X = 5.4857990945E-4_WP
END PROGRAM TEST


In 2013 there was a question on converting a big working code from double to quadruple precision: "Converting a working code from double-precision to quadruple-precision: How to read quadruple-precision numbers in FORTRAN from an input file", and the consensus was to declare variables using an adjustable parameter "WP" that specifies the "working precision", instead of having a separate version of the program with variables declared using D+01, and another version using Q+01. This way we can easily switch back and forth by defining WP=real128 or WP=real64 at the top, and the rest doesn't need to change.

But how do we do this?

I tried the suggestion in the answer to that question, by making a simple code TEST.F90:

  PROGRAM TEST
  use ISO_FORTRAN_ENV
  WP= real128
  IMPLICIT NONE
  real (WP) ::  X
  X= 5.4857990945E-4_WP
  END PROGRAM TEST

compiled with:

~/gcc-4.6/bin/gfortran -o tst.x TEST.F90

But it gives:

      IMPLICIT NONE
                   1
Error: Unexpected IMPLICIT NONE statement at (1)
QLEVEL16.F90:5.12:

      real (WP) ::  MEL 
            1
Error: Parameter 'wp' at (1) has not been declared or is a variable, which does not reduce to a constant expression
QLEVEL16.F90:6.29:

      MEL= 5.4857990945E-4_WP
                             1
Error: Missing kind-parameter at (1)

解决方案

The kind specifier must be an integer parameter - and you do not declare it appropriately. Furthermore, implicit none must go before any declaration.

Here is a working version addressing both issues:

PROGRAM TEST
  use ISO_FORTRAN_ENV
  IMPLICIT NONE
  integer, parameter :: WP= real128
  real (WP) ::  X

  X= 5.4857990945E-4_WP
END PROGRAM TEST

这篇关于我如何声明一个数字的精度是一个可调参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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