Bug #10850
closedBigDecimal division incorrect
Description
I tried this in Ruby 2.1.4, 2.1.5 and 2.2.0, but in none of the versions it seems correct to me.
irb(main):041:0* amount1 = BigDecimal("241.3")
=> #BigDecimal:7f49bcb03558,'0.2413E3',18(18)
irb(main):042:0> amount2 = BigDecimal("1800")
=> #BigDecimal:7f49bcaf3400,'0.18E4',9(18)
irb(main):043:0> rate = amount1 / amount2
=> #<BigDecimal:7f49bcae8398,'0.1340555555 5555555555 5555556E0',27(45)>
irb(main):044:0> rate * amount2 #should return amount1 = 241.3 in BigDecimal, but it does not
=> #<BigDecimal:7f49bcad6a30,'0.2413000000 0000000000 00000008E3',36(45)>
My guess is that there is a bug in the BigDecimal implementation.
Updated by shugo (Shugo Maeda) almost 10 years ago
- Priority changed from 5 to Normal
Elyasin Shaladi wrote:
I tried this in Ruby 2.1.4, 2.1.5 and 2.2.0, but in none of the versions it seems correct to me.
irb(main):041:0* amount1 = BigDecimal("241.3")
=> #BigDecimal:7f49bcb03558,'0.2413E3',18(18)irb(main):042:0> amount2 = BigDecimal("1800")
=> #BigDecimal:7f49bcaf3400,'0.18E4',9(18)irb(main):043:0> rate = amount1 / amount2
=> #<BigDecimal:7f49bcae8398,'0.1340555555 5555555555 5555556E0',27(45)>irb(main):044:0> rate * amount2 #should return amount1 = 241.3 in BigDecimal, but it does not
=> #<BigDecimal:7f49bcad6a30,'0.2413000000 0000000000 00000008E3',36(45)>My guess is that there is a bug in the BigDecimal implementation.
The result of 241.3 / 1800 is a recurring decimal 0.1340555555.....,
which can't be represented exactly by a BigDecimal.
Please use Rational if you need an exact representation of 241.3 / 1800.
Updated by Elyasin (Elyasin Shaladi) almost 10 years ago
Thanks for the answer Shugo,
however I am not sure if that is easily possible for me in that case. I use BigDecimal as a fiedl type in a Rails application and don't see how I can ues Rational here :-(
Any suggestions or tips or references? would be very helpful.
Updated by shugo (Shugo Maeda) almost 10 years ago
- Status changed from Open to Rejected
Elyasin Shaladi wrote:
Thanks for the answer Shugo,
however I am not sure if that is easily possible for me in that case. I use BigDecimal as a fiedl type in a Rails application and don't see how I can ues Rational here :-(
Any suggestions or tips or references? would be very helpful.
You can use BigDecimal#to_r to convert a BigDecimal to a Rational.
Updated by Elyasin (Elyasin Shaladi) over 9 years ago
Maybe you have an advice for the following I am thinking to use.
I could convert BigDecimal to Rational, do the calculation in Rational numbers and then convert that to back to BigDecimal.
Do you see any caveats with this approach?
Shugo Maeda wrote:
Elyasin Shaladi wrote:
Thanks for the answer Shugo,
however I am not sure if that is easily possible for me in that case. I use BigDecimal as a fiedl type in a Rails application and don't see how I can ues Rational here :-(
Any suggestions or tips or references? would be very helpful.You can use BigDecimal#to_r to convert a BigDecimal to a Rational.