【问题标题】:How to change the precision of a value in a pmt pairs如何更改 pmt 对中值的精度
【发布时间】:2021-11-28 23:30:54
【问题描述】:

我正在尝试在 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 对值的精度?

【问题讨论】:

    标签: python python-3.x numpy gnuradio


    【解决方案1】:

    这只是“漂亮打印字符串表示”的精度。当您将 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 转换为本机类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      相关资源
      最近更新 更多