Project

General

Profile

Actions

Bug #585

closed

BigDecimal#remainder is not correct with small divisors

Added by petebd (Pete Bacon Darwin) about 16 years ago. Updated over 13 years ago.

Status:
Closed
Target version:
-
ruby -v:
-
Backport:
[ruby-core:18796]

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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0