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
Source
# File lib/bson/decimal128/builder.rb, line 285 def initialize(big_decimal) @big_decimal = big_decimal end
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
Public Instance Methods
Source
# File lib/bson/decimal128/builder.rb, line 297 def bits if special? to_special_bits else to_bits end end
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
Private Instance Methods
Source
# File lib/bson/decimal128/builder.rb, line 328 def special? @big_decimal.infinite? || @big_decimal.nan? end
Source
# File lib/bson/decimal128/builder.rb, line 319 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
Source
# File lib/bson/decimal128/builder.rb, line 307 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