Project

General

Profile

Actions

Bug #11707

closed

(a * b) / b yields incorrect result when a is BigDecimal & b is Rational with large terms

Added by yipdw (David Yip) over 8 years ago. Updated over 8 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
[ruby-core:71543]

Description

When performing arithmetic on BigDecimals and Rationals, (a * b) / b sometimes doesn't yield a result that is equal to a.

Here is an example program:

require 'bigdecimal'

a = BigDecimal.new('5.0')
b = Rational(8896443230521, 1290320000)

puts ((a * b) / b) == a

On a 64-bit Ubuntu 14.10 installation, the above program prints false; however, as far as I know, both BigDecimal and Rational should be able to preserve enough information to make the program print true. (Under JRuby 9.0.4.0 and Rubinius 2.5.8, the above program prints true.)

(The result of ((a * b) / b) under Ruby 2.2.3p173 is indeed not equal to 5.0 -- instead, it's 4.999999997702340194672728864059963439.)

The rational number in the example program is the conversion factor in phys-units (https://github.com/masa16/phys-units) used to perform pressure conversions to and from pounds per square inch. This behavior was originally seen while performing such a conversion.

Updated by mrkn (Kenta Murata) over 8 years ago

  • Status changed from Open to Rejected

You can convert to Rational from BigDecimal to use BigDecimal#to_r.

Check the following example:

require 'bigdecimal'

a = BigDecimal('5.0')
b = 8896443230521/1290320000r

puts ((a.to_r * b) / b) == a

I think a BigDecimal number shouldn't be converted to a Rational number automatically
because a BigDecimal number has finite precision while a Rational number has infinite precision.

Actions

Also available in: Atom PDF

Like0
Like0