假设我有一个模块:
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所以至少部分是十六进制)。也许是某种十六进制编码的浮点数?
答案 0 :(得分:0)
在@roygvib评论的帮助下,这适用于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()