From: Pete Bacon Darwin Date: 2008-09-23T06:07:17+09:00 Subject: [ruby-core:18796] [Bug #585] BigDecimal#remainder is not correct with small divisors Bug #585: BigDecimal#remainder is not correct with small divisors http://redmine.ruby-lang.org/issues/show/585 Author: Pete Bacon Darwin Status: Open, Priority: Normal 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 ---------------------------------------- http://redmine.ruby-lang.org