class BSON::Decimal128::Builder::FromBigDecimal

@since 4.2.0
@api private
Helper class for parsing a BigDecimal into Decimal128 high and low bits.

def bits

Other tags:
    Since: - 4.2.0

Returns:
  • (Array) - Tuple of the low and high bits.

Other tags:
    Example: Get the bits for the Decimal128 object created from the big decimal. -
def bits
  if special?
    to_special_bits
  else
    to_bits
  end
end

def initialize(big_decimal)

Other tags:
    Since: - 4.2.0

Parameters:
  • big_decimal (BigDecimal) -- The big decimal object to

Other tags:
    Example: Create the FromBigDecimal builder. -
def initialize(big_decimal)
  @big_decimal = big_decimal
end

def special?

def special?
  @big_decimal.infinite? || @big_decimal.nan?
end

def to_bits

def to_bits
  sign, significand_str, _, exp = @big_decimal.split
  exponent = @big_decimal.zero? ? 0 : exp - significand_str.length
  is_negative = (sign == ::BigDecimal::SIGN_NEGATIVE_FINITE || sign == ::BigDecimal::SIGN_NEGATIVE_ZERO)
  Builder.parts_to_bits(significand_str.to_i,
                        exponent,
                        is_negative)
end

def to_special_bits

def to_special_bits
  case @big_decimal.sign
    when ::BigDecimal::SIGN_POSITIVE_INFINITE
      high = INFINITY_MASK
    when ::BigDecimal::SIGN_NEGATIVE_INFINITE
      high = INFINITY_MASK | SIGN_BIT_MASK
    when ::BigDecimal::SIGN_NaN
      high = NAN_MASK
  end
  [ 0, high ]
end