コード例 #1
0
def pack_decimal64(decimal):
    """Pack an IEEE 754-2008 64-bit decimal floating point number."""
    if not decimal.is_finite():
        yield _decimal_pack_special(decimal, 8)
        return
    sign, digits, exponent = decimal.as_tuple()
    if len(digits) > 16 or (not -398 <= exponent <= 369):
        raise ValueError()
    significand = int(''.join(map(str, digits)))
    if significand >> 51 == 4:
        yield ((sign << 63) | (3 << 61) | ((exponent + 398) << 51) |
               (significand & 2251799813685247)).to_bytes(8, 'big')
    else:
        yield ((sign << 63) | ((exponent + 398) << 53) | significand).to_bytes(
            8, 'big')
コード例 #2
0
def pack_decimal32(decimal):
    """Pack an IEEE 754-2008 32-bit decimal floating point number."""
    if not decimal.is_finite():
        yield _decimal_pack_special(decimal, 4)
        return
    sign, digits, exponent = decimal.as_tuple()
    if len(digits) > 7 or (not -101 <= exponent <= 90):
        raise ValueError()
    significand = int(''.join(map(str, digits)))
    if significand >> 21 == 4:
        yield ((sign << 31) | (3 << 29) | ((exponent + 101) << 21) |
               (significand & 2097151)).to_bytes(4, 'big')
    else:
        yield ((sign << 31) | ((exponent + 101) << 23) | significand).to_bytes(
            4, 'big')
コード例 #3
0
ファイル: primitives.py プロジェクト: socialcube/binarize
def pack_decimal64(decimal):
    """Pack an IEEE 754-2008 64-bit decimal floating point number."""
    if not decimal.is_finite():
        yield _decimal_pack_special(decimal, 8)
        return
    sign, digits, exponent = decimal.as_tuple()
    if len(digits) > 16 or (not -398 <= exponent <= 369):
        raise ValueError()
    significand = int(''.join(map(str, digits)))
    if significand >> 51 == 4:
        yield ((sign << 63) | (3 << 61) | ((exponent + 398) << 51) |
               (significand & 2251799813685247)).to_bytes(8, 'big')
    else:
        yield ((sign << 63) | ((exponent + 398) << 53) |
               significand).to_bytes(8, 'big')
コード例 #4
0
ファイル: primitives.py プロジェクト: socialcube/binarize
def pack_decimal32(decimal):
    """Pack an IEEE 754-2008 32-bit decimal floating point number."""
    if not decimal.is_finite():
        yield _decimal_pack_special(decimal, 4)
        return
    sign, digits, exponent = decimal.as_tuple()
    if len(digits) > 7 or (not -101 <= exponent <= 90):
        raise ValueError()
    significand = int(''.join(map(str, digits)))
    if significand >> 21 == 4:
        yield ((sign << 31) | (3 << 29) | ((exponent + 101) << 21) |
               (significand & 2097151)).to_bytes(4,'big')
    else:
        yield ((sign << 31) | ((exponent + 101) << 23) |
               significand).to_bytes(4, 'big')
コード例 #5
0
def pack_decimal128(decimal):
    """Pack an IEEE 754-2008 128-bit decimal floating point number."""
    if not decimal.is_finite():
        yield _decimal_pack_special(decimal, 16)
        return
    sign, digits, exponent = decimal.as_tuple()
    if len(digits) > 34 or (not -6176 <= exponent <= 6111):
        raise ValueError()
    significand = int(''.join(map(str, digits)))
    if significand >> 111 == 4:
        yield ((sign << 127) | (3 << 125) | ((exponent + 6176) << 111) |
               (significand & 2596148429267413814265248164610047)).to_bytes(
                   16, byteorder='big')
    else:
        yield ((sign << 127) | ((exponent + 6176) << 113)
               | significand).to_bytes(16, byteorder='big')
コード例 #6
0
ファイル: primitives.py プロジェクト: socialcube/binarize
def pack_decimal128(decimal):
    """Pack an IEEE 754-2008 128-bit decimal floating point number."""
    if not decimal.is_finite():
        yield _decimal_pack_special(decimal, 16)
        return
    sign, digits, exponent = decimal.as_tuple()
    if len(digits) > 34 or (not -6176 <= exponent <= 6111):
        raise ValueError()
    significand = int(''.join(map(str, digits)))
    if significand >> 111 == 4:
        yield ((sign << 127) | (3 << 125) | ((exponent + 6176) << 111) |
               (significand & 2596148429267413814265248164610047)
               ).to_bytes(16, byteorder='big')
    else:
        yield ((sign << 127) | ((exponent + 6176) << 113) |
               significand).to_bytes(16, byteorder='big')