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.

Yes, in the case of BigDecimal we have a value representing "precisely" 
the value 0.33333333, but that result is obviously less accurate than 
the result of the floating point calculation.

Over large aggregations this can make a difference. It is certainly the 
case that IEEE floats introduce error due to the inherent storage 
limitations, but doing precise mathematics as fixed point has it's own 
complications as well.

Ruby's BigDecimal class does not provide the needed accuracy for complex 
mathematics. The difference is that BigDecimal always stores precise 
values within the limits of it storage space. Where floats store 
imprecise representations, but representations with a fixed precision.

I don't think either of these classes are appropriate for the sort of 
math mentioned above. But, they both have their uses.

-- 
Posted via http://www.ruby-forum.com/.

-- 
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.

Reply via email to