如何更改 pmt 对中值的精度

How to change the precision of a value in a pmt pairs

我正在尝试在 gnuradio 中实现一个块。它必须向输出发送消息并命令信号源块的频率。该消息是一个 pmt 对,它在 cdr 字段中包含频率值。当我把它成对的时候,它的精度下降了很多。例如,使用此代码

import numpy as np
import pmt

freqval = np.float64(123494235.48994166)
msg = pmt.cons(pmt.intern("freq"), pmt.to_pmt(freqval))

print("freqval : ", freqval)
print("msg : ", msg)

freqval = np.float64(123.49423548994166)
msg = pmt.cons(pmt.intern("freq"), pmt.to_pmt(freqval))

print("freqval : ", freqval)
print("msg : ", msg)

我得到

freqval :  123494235.48994166
msg :  (freq . 1.23494e+08)
freqval :  123.49423548994166
msg :  (freq . 123.494)

而我希望 msg 中的值与 freqval 中包含的值相同。 我试图用 from_float() 和 from_double() 替换 to_pmt(),但没有任何改变,并将值设为字符串,但 gnuradio 发出警告

gr::log :WARN: sig_source0 - frequency value needs to be a number

有没有办法改变 pmt 对值的精度?

这只是“漂亮的打印字符串表示”的精度。当您将 PMT 转换回 python float 或 C double 时,您应该获得足够的精度。

> value = 1/3
# one third has very many fractional digits – it 
# can't be exactly represented in base 10
# but it can't even be exactly stored in a float,
# because 1/3 can also not be exactly represented
# in binary.
> print(value)
0.3333333333333333
# note how most of these digits are just
# "made up (i.e. the closest approximation to 1/3
# in binary of given length)"; python floating points
# don't have 50 significant digits!
> print(f"{value:.50f}"
0.33333333333333331482961625624739099293947219848633
> a = pmt.pmt_to_python.python_to_pmt(value)
> b = pmt.pmt_to_python.pmt_to_python(a)
> print(b-value)
0.0
> print(f"{b-value:.50f}")
0.00000000000000000000000000000000000000000000000000

对于大多数浮点格式来说,这基本上是正确的:打印数字的方式不等于数字本身!

PMT 知道这一点,并决定在打印时不尝试表示很多数字——这实际上更方便。当您需要使用 PMT 的值时,您总是会再次将该 PMT 转换为本机类型。