Project

General

Profile

Actions

Bug #585

closed

BigDecimal#remainder is not correct with small divisors

Added by petebd (Pete Bacon Darwin) over 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 #1

Updated by shyouhei (Shyouhei Urabe) almost 16 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

=begin

=end

Actions #2

Updated by marcandre (Marc-Andre Lafortune) over 15 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
Applied in changeset r25020.
=end

Actions #3

Updated by marcandre (Marc-Andre Lafortune) over 15 years ago

  • Category set to doc
  • Status changed from Closed to Open
  • ruby -v set to -

=begin
Documentation was incorrect.

x.remainder(y) = x - (x/y).truncate

Among other things, if y divides x, then modulo == remainder == 0 no matter what the signs involved.
=end

Actions #4

Updated by marcandre (Marc-Andre Lafortune) over 15 years ago

  • Status changed from Open to Closed

=begin

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0