From: muraken@... Date: 2015-12-25T06:30:49+00:00 Subject: [ruby-core:72475] [Ruby trunk - Bug #11707] [Rejected] (a * b) / b yields incorrect result when a is BigDecimal & b is Rational with large terms Issue #11707 has been updated by Kenta Murata. 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. ---------------------------------------- Bug #11707: (a * b) / b yields incorrect result when a is BigDecimal & b is Rational with large terms https://bugs.ruby-lang.org/issues/11707#change-55762 * Author: David Yip * Status: Rejected * Priority: Normal * Assignee: * ruby -v: ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux] * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN ---------------------------------------- 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. -- https://bugs.ruby-lang.org/ Unsubscribe: