On 24 December 2010 03:59, Marnen Laibow-Koser <[email protected]> wrote:
> Robert Walker wrote in post #970390:
>> Marnen Laibow-Koser wrote in post #970377:
>>> Then use fixed-point math. IEEE floats are 100% inappropriate for any
>>> sort of mathematical calculation. And who cares how fast they are if
>>> they're inaccurate? Inaccurate engineering calculations are worse than
>>> none at all -- do *you* want to drive over that bridge? :)
>>
>> Let's break this down to a comparison of accuracy vs. precision.
>>
>> Take for example the value 1/3.
>>
>> First using floating point math:
>>
>> x = 1.0
>> => 1.0
>> y = 3.0
>> => 3.0
>> z = x / y
>> => 0.3333333333333333
>>
>> Now with Ruby's BigDecimal math:
>> x = BigDecimal.new('1')
>> => #<BigDecimal:102c078,'0.1E1',4(8)>
>> y = BigDecimal.new('3')
>> => #<BigDecimal:10388f0,'0.3E1',4(8)>
>> z = x / y
>> => #<BigDecimal:103680c,'0.33333333E0',8(16)>
>>
>> In the case of float we have 16 decimal digits of precision, but in the
>> case of BigDecimal we have only eight digits of precision.
>
> Are you sure? Or are only eight digits displayed?
$ rails console
Loading development environment (Rails 3.0.3)
ruby-1.9.2-p0 > x=BigDecimal('1')/BigDecimal('3')
=> #<BigDecimal:ad823a4,'0.33333333E0',8(16)>
ruby-1.9.2-p0 > x*BigDecimal('3')
=> #<BigDecimal:ad2b270,'0.99999999E0',8(20)>
ruby-1.9.2-p0 > x*BigDecimal('3') == 1
=> false
ruby-1.9.2-p0 > x*BigDecimal('3') - 1
=> #<BigDecimal:aa4fdf8,'-0.1E-7',4(20)>
Colin
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.