Bug #585
closedBigDecimal#remainder is not correct with small divisors
Description
=begin
require 'bigdecimal'
According to the documentation: BigDecimal#remainder is BigDecimal#modulus minus the value divided by, if values have opposite sign¶
@mixed = BigDecimal("1.23456789")
@pos_int = BigDecimal("2E5555")
@neg_int = BigDecimal("-2E5555")
@pos_frac = BigDecimal("2E-9999")
@neg_frac = BigDecimal("-2E-9999")
One would expect that¶
puts @mixed.remainder(@neg_frac) == (@mixed % @neg_frac) - @neg_frac # 0.2E-9998
puts @pos_int.remainder(@neg_frac) == (@pos_int % @neg_frac) - @neg_frac # 0.2E-9998
puts @neg_frac.remainder(@pos_int) == (@neg_frac % @pos_int) - @pos_int # -0.2E-9998
puts @neg_int.remainder(@pos_frac) == (@neg_int % @pos_frac) - @pos_frac # -0.2E-9998
But in actual fact¶
puts @mixed.remainder(@neg_frac) == BigDecimal("0.0")
puts @pos_int.remainder(@neg_frac) == BigDecimal("0.0")
puts @neg_frac.remainder(@pos_int) == BigDecimal("-0.2E-9998")
puts @neg_int.remainder(@pos_frac) == BigDecimal("-0.0")
This appears to be due to some premature rounding optimization in divmod¶
=end