Bug #19321
closedBigDecimal multiplication giving different result since Ruby 3.1
Description
I have this math operation 120.to_d * 0.07 and I noticed that since upgrading to Ruby 3.1 I get different results. I would expect to get the same result as in previous versions.
rbenv local 3.0.3
➜ irb
require 'bigdecimal/util.rb'
(120.to_d * 0.07).to_s('F')
=> "8.4000000000000012"
rbenv local 3.1.3
✗ irb
require 'bigdecimal/util.rb'
(120.to_d * 0.07).to_s('F')
=> "8.4"
It looks like the change has been introduced in Ruby 3.1. I tested Ruby 2.7.0 and I get the same result as in 3.0 ("8.4000000000000012")
I saw in the release notes that there were changes to bigdecimal so I am not sure if this is expected or it's a bug
Updated by nobu (Nobuyoshi Nakada) almost 2 years ago
The newer result seems arithmetically correct.
Updated by jeremyevans0 (Jeremy Evans) almost 2 years ago
- Status changed from Open to Closed
The old result was incorrect. The new, correct result is due to a bug fix. I'm going to close this. If you can find a result that is incorrect in Ruby 3.1 or 3.2, please post it.
Updated by nobu (Nobuyoshi Nakada) almost 2 years ago
JFI: it is https://github.com/ruby/bigdecimal/pull/180, which fixed https://github.com/ruby/bigdecimal/issues/70 and #13331.
Updated by alexandruanca (Alexandru Anca) almost 2 years ago
The old result was incorrect. The new, correct result is due to a bug fix. I'm going to close this. If you can find a result that is incorrect in Ruby 3.1 or 3.2, please post it.
I see, makes sence. Thanks for the explanation