2017-06-15 8 views
1

私は、パッケージを使用したいbitstringmpmath(または任意に設定した出力精度を保証する他の方法と丸めモードを指定)rcpsqrtsinco S、lnexpの値を計算するために...フロートに入力はバイナリbitstringとして与えられ、バイナリbitstring答えを得る。bitstringパッケージで '0'と '1'文字の文字列としてfloatを入力するにはどうすればいいですか?

これはC言語のMPFRですが、扱いやすさを考慮して、Pythonの浮動小数点高精度パッケージを探求したいと考えています。

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

、すなわちどのように(わずか)としてそれを解釈する方法のいずれかで bitstringまたは mpmath'00111111100110011001100110011010'を養うために 1.2し、その後にそれを養う:私の最初の問題は、 bitstring変換に次の小数floatを逆にする方法です sincos、または lnのような関数(私の答えは bitstringに再度変換されます)。

私はそれは難しいのPython bitstring/mpmathドキュメントからバイナリ入力について学ぶために探しています。小数浮動表記の難しさについては言いますが、これらをバイパスする方法ではなく、正確なバイナリ浮動小数点の入力だけではありません。

答えて

2

BitArraybinパラメータを取り、それはバイナリ文字列表現からそれをitialises:だから

>>> 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

BitArrayドキュメンテーション文字列によると、あなたはbin引数を指定することができます:float値を取得する

__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 
>>> 
関連する問題