Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
gfortran模块文件中的参数编码_Fortran_Gfortran - Fatal编程技术网

gfortran模块文件中的参数编码

gfortran模块文件中的参数编码,fortran,gfortran,Fortran,Gfortran,假设我有一个模块: module testParam real, parameter :: x=1.0, xx=10.0 real, parameter :: pi=3.14 end module testParam 用 gfortran -c testParam.f90 并查看生成的模块文件 cp testparam.mod testparam.gz gunzip testparam.gz 修剪后的输出为: (2 'pi' 'testparam' '' 1 ((PAR

假设我有一个模块:

module testParam

    real, parameter :: x=1.0, xx=10.0
    real, parameter :: pi=3.14

end module testParam

gfortran -c testParam.f90
并查看生成的模块文件

cp testparam.mod testparam.gz
gunzip testparam.gz
修剪后的输出为:

(2 'pi' 'testparam' '' 1 ((PARAMETER UNKNOWN-INTENT UNKNOWN-PROC UNKNOWN
IMPLICIT-SAVE 0 0) () (REAL 4 0 0 0 REAL ()) 0 0 () (CONSTANT (REAL 4 0
0 0 REAL ()) 0 '0.323d70c@1') () 0 () () 0 0)

4 'x' 'testparam' '' 1 ((PARAMETER UNKNOWN-INTENT UNKNOWN-PROC UNKNOWN
IMPLICIT-SAVE 0 0) () (REAL 4 0 0 0 REAL ()) 0 0 () (CONSTANT (REAL 4 0
0 0 REAL ()) 0 '0.1000000@1') () 0 () () 0 0)

5 'xx' 'testparam' '' 1 ((PARAMETER UNKNOWN-INTENT UNKNOWN-PROC UNKNOWN
IMPLICIT-SAVE 0 0) () (REAL 4 0 0 0 REAL ()) 0 0 () (CONSTANT (REAL 4 0
0 0 REAL ()) 0 '0.a000000@1') () 0 () () 0 0)
然后我们可以看到x的值已存储为“0”。1000000@1'和pi已存储为'0'。323d70c@1'和xx作为'0'。a000000@1'. 如何将字符串编码的参数转换回数字


考虑到x项的值,我最初假设它只是一种格式a@b,a*10^b,但pi的值是十六进制编码的(对于xx变量int(0xa)==10.0,因此至少有一部分是十六进制)。也许它是某种十六进制编码的浮点数?

在@roygvib comment的帮助下,它适用于python

def hextofloat(s):
    # Given a hex like parameter '0.12decde@9' returns 5065465344.0
    man, exp =s.split('@')
    exp=int(exp)
    decimal = man.index('.')
    man = man[decimal+1:]
    man = man.ljust(exp,'0')
    man = man[:exp]+'.'+man[exp:]
    man = man +'P0'
    return float.fromhex(man)

将字符串值拆分为mantisa(0.12decde)和指数(9),将十六进制数放在小数点(12decde)右侧,并用足够的零(12decde00)填充它,使其至少与指数一样长,将小数点放回(12decde00.0),然后在末尾放一个'P0'(12decde00.P0)并传递到float.fromhex()

哪个版本的gfortran?尽管说实话,我的方法可能是
使用testParam;打印*,x;结束
etc.5.3.1,我也希望如此,但我正在编写一些代码,对mod文件进行整理,以了解模块在不“运行”它的情况下正在做什么。在我看来,这就像是一种转储二进制形式的单精度浮点值,并对位进行某种程度的分解。“0.”和“@”之间的部分显然是尾数。我可能猜“@1”以某种形式或方式表示指数,但如果数字的指数为非零,则更为清晰。您可以阅读gfortran源代码的相关部分。似乎我们可以通过键入0x3.23d70cP0(在Julia的回复中,如果您安装了它)来检查数字,给出了3.140000104904175。对于较大的数字,如果您在模块文件中使用实数(8)、参数::A=1234567890.12345d0等,我们可以将十六进制输出转换为与原始输出类似的输出(根据@*移动小数点后)。根据需要的通用性,您还需要使用Inf、NaN以及可能的+/-0和非规范化进行测试(最后两个可能还可以)。