首页 > 解决方案 > Python中的浮点字符串到IEEE 754 binary128

问题描述

我想要这样的功能:

>>> binary128_to_hex("1.0")
'3fff0000000000000000000000000000'

我目前使用 C 并qemu-aarch64在我的 x86 笔记本电脑上执行此操作。如何“本机”实现这样的功能?我发现numpy.float128包裹struct没有帮助。
此外,感谢Mark Dickinson 的回答,我想出进行反向转换(尽管只处理标准化值):

import decimal


def hex_to_binary128(x: str):
    with decimal.localcontext() as context:
        context.prec = 34
        x = int(x, 16)
        significand_mask = (1 << 112) - 1
        exponent_mask = (1 << 127) - (1 << 112)
        trailing_significand = x & significand_mask
        significand = 1 + decimal.Decimal(trailing_significand) / (1 << 112)
        biased_exponent = (x & exponent_mask) >> 112
        exponent = biased_exponent - 16383
        f = significand * decimal.Decimal(2) ** exponent
        return f


if __name__ == "__main__":
    print(hex_to_binary128("0000ffffffffffffffffffffffffffff"))
    # 3.362103143112093506262677817321752E-4932

标签: pythonfloating-pointieee-754arbitrary-precision

解决方案


这里有一系列可能的解决方案,具体取决于您希望允许多少复杂性、您需要什么样的性能、您准备在多大程度上依赖外部库以及您需要在多大程度上处理 IEEE 754 特殊情况(溢出、次正规、有符号零等)。

这是一些工作代码,我希望能给出合理的折衷方案。它 (a) 相当简单,(b) 不依赖于标准库之外的任何东西,(c) 可以很好地处理次正规、有符号零和溢出(但不会尝试解释像“inf”或“nan”这样的字符串"),并且 (d) 的性能可能很糟糕。但是,如果您只对休闲用途感兴趣,那可能就足够了。

该代码的想法是通过使用fractions.Fraction解析器将字符串输入解析到Fraction对象来回避所有解析困难。然后,我们可以分离该Fraction对象并构建我们需要的信息。

我将分三部分介绍解决方案。首先,我们需要的基本工具之一是能够计算正数的二进制指数(换句话说,以 2 为底的对数的底)Fraction。这是代码:

def exponent(f):
    """
    Binary exponent (IEEE 754 style) of a positive Fraction f.

    Returns the unique integer e such that 2**e <= f < 2**(e + 1). Results
    for negative or zero f are not defined.
    """
    n, d = f.numerator, f.denominator
    e = n.bit_length() - d.bit_length()
    if e >= 0:
        adjust = (n >> e) < d     # n / d < 2**e  <=>  floor(n / 2**e) < d
    else:
        adjust = (-d >> -e) < -n  # n / d < 2**e  <=>  floor(-d / 2**-e) < -n
    return e - adjust

这很简单:e分子和分母的位长差异要么给我们正确的指数,要么比它应该的大一个。要弄清楚哪个,我们必须将分数的值与 进行比较2**e,其中e是我们的测试指数。2**e我们可以直接这样做,先计算Fraction然后比较,但是使用一些位移会更有效,所以这就是我们所做的。

接下来,我们定义了一些描述 IEEE 754 binary128 格式的基本常量和派生常量。(这使得测试下面的代码变得很容易,通过将这些常量替换为 binary64 格式的常量并检查结果是否符合预期。)格式的位宽是128; 精度是113,其他一切都可以从这两个值中得出。

# Basic and derived constants for the format.
WIDTH = 128
PRECISION = 113
EMAX = (1 << WIDTH - PRECISION - 1) - 1
EMIN = 1 - EMAX
INF = (1 << WIDTH - 1) - (1 << PRECISION - 1)
SIGN_BIT = 1 << WIDTH - 1

其中大部分应该是不言自明的。该常量INF是正无穷大常量的位表示,我们将使用它来处理溢出。

最后,这是主要功能:

from fractions import Fraction as F

def binary128_to_hex(s):
    """
    Convert a decimal numeric string to its binary128 representation.

    Given a decimal string 's' (for example "1.2", or "-0.13e-123"), find the
    closest representable IEEE 754 binary128 float to the value represented
    by that string, and return a hexadecimal representation of the bits
    of that float in the corresponding IEEE 754 interchange format.
    """
    # Convert absolute value to a Fraction. Capture the sign separately.
    f, negative = abs(F(s)), s.lstrip().startswith("-")

    # Find the bits representing the significand and exponent of the result.
    if f == 0:
        bits = 0  # Handle special case of zero.
    else:
        # Find exponent; adjust for possible subnormal.
        exp = max(exponent(f), EMIN)
        if exp > EMAX:
            bits = INF  # Overflow to infinity
        else:
            significand = round(f / F(2) ** (exp + 1 - PRECISION))
            bits = (exp - EMIN << PRECISION - 1) + significand

    # Merge sign bit if necessary, then format as a hex string.
    if negative:
        bits |= SIGN_BIT
    return f'{bits:0{WIDTH//4}x}'

上面有两件鬼鬼祟祟的花招值得特别一提:首先,在构造bits和使用表达式时exponent,我们没有做任何特别的事情来处理次正规。尽管如此,代码仍然正确处理了次正规:对于正常情况,指数值实际上比它应该小一,但是当我们执行加法时,有效数的最高有效位最终会增加指数字段。(所以重要的是我们使用而不是将指数部分与有效数字结合起来。)significand(exp - EMIN << PRECISION - 1) + significandexp - EMIN+|

另一个观察结果是,虽然选择exp确保调用的参数round严格小于2**PRECISION,但调用的结果可能恰好round2**PRECISION。那时您可能希望我们必须针对这种情况进行测试,并相应地调整指数和有效数。但同样,没有必要特别处理这种情况 - 当使用 组合字段时(exp - EMIN << PRECISION - 1) + significand,我们得到指数字段的额外增量并且一切正常,即使在我们最终溢出到无穷大的极端情况下也是如此。IEEE 754 二进制交换格式的优雅设计使这种诡计成为可能。

以下是在几个示例上测试上述代码的结果:

>>> binary128_to_hex("1.0")
'3fff0000000000000000000000000000'
>>> binary128_to_hex("-1.0")
'bfff0000000000000000000000000000'
>>> binary128_to_hex("-0.0")
'80000000000000000000000000000000'
>>> binary128_to_hex("3.362103143112093506262677817321752E-4932")
'0000ffffffffffffffffffffffffffff'
>>> binary128_to_hex("1.1897314953572317650857593266280071308E+4932")
'7fff0000000000000000000000000000'
>>> binary128_to_hex("1.1897314953572317650857593266280070162E+4932")
'7ffeffffffffffffffffffffffffffff'
>>> binary128_to_hex("1.2345E-4950")  # subnormal value
'00000000000000000006c5f6731b03b8'
>>> binary128_to_hex("3.14159265358979323846264338327950288")
'4000921fb54442d18469898cc51701b8'

推荐阅读