module BSON::Decimal128::Builder
def parts_to_bits(significand, exponent, is_negative)
- Since: - 4.2.0
Returns:
-
(Array)
- Tuple of the low and high bits.
Parameters:
-
is_negative
(true, false
) -- Whether the value is negative. -
exponent
(Integer
) -- The exponent. -
significand
(Integer
) -- The significand.
def parts_to_bits(significand, exponent, is_negative) validate_range!(exponent, significand) exponent = exponent + Decimal128::EXPONENT_OFFSET high = significand >> 64 low = (high << 64) ^ significand if high >> 49 == 1 high = high & 0x7fffffffffff high |= TWO_HIGHEST_BITS_SET high |= (exponent & 0x3fff) << 47 else high |= exponent << 49 end if is_negative high |= SIGN_BIT_MASK end [ low, high ] end
def valid_exponent?(exponent)
def valid_exponent?(exponent) exponent <= Decimal128::MAX_EXPONENT && exponent >= Decimal128::MIN_EXPONENT end
def valid_significand?(significand)
def valid_significand?(significand) significand.to_s.length <= Decimal128::MAX_DIGITS_OF_PRECISION end
def validate_range!(exponent, significand)
def validate_range!(exponent, significand) unless valid_exponent?(exponent) raise Error::InvalidDecimal128Range.new end unless valid_significand?(significand) raise Error::UnrepresentablePrecision.new end end