简体   繁体   中英

Wrong Ruby Float and BigDecimal subtraction result

I am running:

[~/ruby/rails/sas]$ ruby --version
ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]

on Mac Snow Leopard 10.6.3

Can anyone help to explain why the Float and BigDecimal subtraction can be this wrong.

[~/ruby/rails/sas]$ console
Loading development environment (Rails 2.1.1)
>> num = 30.0
=> 30.0
>> num.class
=> Float
>> ex = 28.04.to_d
=> #<BigDecimal:105367e40,'0.284E2',8(8)>
>> ex.class
=> BigDecimal
>> num - ex
=> 1.6
>> _.class
=> Float
>> 

I was hoping that the result should be 1.96, I know that perhaps doing an arithmetic operation using 2 different data types are not recommended, but this behavior is so strange.

It seems to be wise that from now on, I have to check the variables data type before doing any arithmetic operation.

Hopefully somebody can give me an insight on what was happening.

It's the issue with to_d method in Rails 2.1.1. I tried with

ex = BigDecimal.new '28.04'
#=> #<BigDecimal:1209328,'0.2804E2',8(8)>

and with Rails 2.3.5

 ex = 28.04.to_d
 #=> #<BigDecimal:219ea18,'0.2804E2',8(8)>

and num - ex #=> 1.96

worked fine

So you have three options either upgrade your rails version or override the to_d method or just use BigDecimal.new '28.04' instead of to_d

The problem is not rails 2.1.1, since I tried with rails 2.3.8 with the same result

[~/ruby/rails/sample_2.3.8]$ script/console
Loading development environment (Rails 2.3.8)
>> BigDecimal("28.04")
=> #<BigDecimal:1033eccc8,'0.284E2',8(8)>
>> 

I am very sure that the problem is Ruby 1.8.7 p173 that comes standard with Snow Leopard 10.6.3 as posted here:

http://redmine.ruby-lang.org/issues/show/1910

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM