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?
>
> 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.
Not obvious at all, since I don't know how many 3s the BigDecimal is
*actually* storing.
>
> Over large aggregations this can make a difference.
Yes, it certainly can.
> 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.
Sure. BigDecimal is the best of both worlds.
>
> Ruby's BigDecimal class does not provide the needed accuracy for complex
> mathematics.
Of course it does. Why do you think it does not?
Anyway, if you need more than BigDecimal, then start climbing into the
realms of symbolic math with Rational.
> 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.
Why is BigDecimal inappropriate? What would you use instead?
> But, they both have their uses.
I'm not sure Float has a proper use case at all. :)
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
Sent from my iPhone
--
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.