class BSON::Decimal128::Builder::FromBigDecimal

Helper class for parsing a BigDecimal into Decimal128 high and low bits.

@api private

@since 4.2.0

Public Class Methods

new(big_decimal) click to toggle source

Initialize the FromBigDecimal Builder object.

@example Create the FromBigDecimal builder.

Builder::FromBigDecimal.new(big_decimal)

@param [ BigDecimal ] big_decimal The big decimal object to

create a Decimal128 from.

@since 4.2.0

# File lib/bson/decimal128/builder.rb, line 280
def initialize(big_decimal)
  @big_decimal = big_decimal
end

Public Instance Methods

bits() click to toggle source

Get the bits representing the Decimal128 that the big decimal corresponds to.

@example Get the bits for the Decimal128 object created from the big decimal.

builder.bits

@return [ Array ] Tuple of the low and high bits.

@since 4.2.0

# File lib/bson/decimal128/builder.rb, line 292
def bits
  if special?
    to_special_bits
  else
    to_bits
  end
end

Private Instance Methods

special?() click to toggle source
# File lib/bson/decimal128/builder.rb, line 323
def special?
  @big_decimal.infinite? || @big_decimal.nan?
end
to_bits() click to toggle source
# File lib/bson/decimal128/builder.rb, line 314
def to_bits
  sign, significand_str, base, 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
to_special_bits() click to toggle source
# File lib/bson/decimal128/builder.rb, line 302
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