【问题标题】:How to input a float as a string of '0' and '1' characters with the bitstring package?如何使用 bitstring 包将浮点数作为“0”和“1”字符的字符串输入?
【发布时间】:2017-11-18 05:41:18
【问题描述】:

我想使用包bitstringmpmath(或任何其他方式来保证任意设置的输出精度和指定的舍入模式)来计算rcpsqrtsin、@的值987654326@s, ln, exp,... 在作为二进制 bitstrings 给出的浮点输入上并获得二进制 bitstring 答案。

我的首选是 C 中的 MPFR,但我想探索 Python 的浮点高精度包,希望更容易处理。我的第一个问题是如何将以下十进制浮点数反转为bitstring 转换:

>>> from bitstring import *
>>> a = BitArray(float=1.2,length=32)
>>> a.bin
'00111111100110011001100110011010'

即如何将'00111111100110011001100110011010' 提供给bitstringmpmath,使其解释为(刚刚结束)1.2,然后将其提供给sincos 或@987654339 等函数@(再次将我的答案变成bitstring)。

我发现很难从 Python bitstring/mpmath 文档中了解二进制输入。它只说明了十进制浮点数表示的困难,而不是如何绕过这些简单地输入精确的二进制浮点数。

【问题讨论】:

    标签: python binary floating-point bitstring mpmath


    【解决方案1】:

    BitArray 采用bin 参数,该参数从二进制字符串表示形式对其进行初始化:

    >>> from bitstring import *
    >>> a = BitArray(float=1.2, length=32)
    >>> a.bin
    '00111111100110011001100110011010'
    >>> b = BitArray(bin=a.bin)
    >>> b.float
    1.2000000476837158
    

    所以一个通用的函数来做到这一点:

    def float_from_bitstring(bitstring):
        return BitArray(bin=bitstring).float
    

    【讨论】:

      【解决方案2】:

      根据BitArraydoc字符串,可以指定bin参数:

      __init__(self, auto=None, length=None, offset=None, **kwargs)
          Either specify an 'auto' initialiser:
          auto -- a string of comma separated tokens, an integer, a file object,
                  a bytearray, a boolean iterable or another bitstring.
      
          Or initialise via **kwargs with one (and only one) of:
          bytes -- raw data as a string, for example read from a binary file.
          bin -- binary string representation, e.g. '0b001010'.   <--------------
          hex -- hexadecimal string representation, e.g. '0x2ef'
          oct -- octal string representation, e.g. '0o777'.
          uint -- an unsigned integer.
          int -- a signed integer.
          float -- a floating point number.
          uintbe -- an unsigned big-endian whole byte integer.
          intbe -- a signed big-endian whole byte integer.
          floatbe - a big-endian floating point number.
          uintle -- an unsigned little-endian whole byte integer.
          intle -- a signed little-endian whole byte integer.
          floatle -- a little-endian floating point number.
          uintne -- an unsigned native-endian whole byte integer.
          intne -- a signed native-endian whole byte integer.
          floatne -- a native-endian floating point number.
          se -- a signed exponential-Golomb code.
          ue -- an unsigned exponential-Golomb code.
          sie -- a signed interleaved exponential-Golomb code.
          uie -- an unsigned interleaved exponential-Golomb code.
          bool -- a boolean (True or False).
          filename -- a file which will be opened in binary read-only mode.
      
          Other keyword arguments:
          length -- length of the bitstring in bits, if needed and appropriate.
                    It must be supplied for all integer and float initialisers.
          offset -- bit offset to the data. These offset bits are
                    ignored and this is intended for use when
                    initialising using 'bytes' or 'filename'.
      

      >>> a = BitArray(bin='00111111100110011001100110011010')
      >>> a.bin
      '00111111100110011001100110011010'
      

      使用float属性获取浮点值:

      >>> a.float
      1.2000000476837158
      >>> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-02
        • 2016-01-03
        • 1970-01-01
        • 2022-11-22
        • 1970-01-01
        • 2011-11-25
        相关资源
        最近更新 更多