def _from_decimal(cls, decimal): """ Create a cudf.Decimal64Dtype from a decimal.Decimal object """ metadata = decimal.as_tuple() precision = max(len(metadata.digits), -metadata.exponent) return cls(precision, -metadata.exponent)
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')
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')
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')
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')
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')
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')
def split_decimal(cls, decimal): t = decimal.as_tuple() i = 0 for d in t[1]: i = i * 10 + d return i, t[2]