On 25 December 2010 00:37, Peter Vandenabeele <li...@ruby-forum.com> wrote:
> Possible solutions I see on top of my head: > A do nothing (the result of division has a "large" precision, > but is not "exact") > B raise an exception if the representation becomes not exact > C set an instance variable in the BigDecimal object that marks > it is "not exact" (like the "tainted" flag) > D let BigDecimal return a Rational if the result can not be > exactly represented as a BigDecimal > E use the Rational class if you want exact divisions > ... > > They all have their advantages: > * A OK. Understanding the behaviour is enough. I understand > now that +.-,* work as expected and / causes the risk > to loose exact representation. > > * B (could be Class or per object flag) > ==> in bookkeeping applications, exactness is required, > so any calculation that destroys the exactness must > be flagged. So if you calculate the "average" profit > over your 7 divisions, you would get an exception, > unless you did something special and understand the risks > > * C this is really unintrusive and allows one to check at the > end if the exactness did not get lost along the road > > * E this exists, use it if you want an "exact" representation > of 1/3. > > I am not in favor of D because it silently changes the class > of the result and this violates the principle of least surprise. > If you want Rational, use it from the start then. For D there is no reason why the class of the result needs to change to Rational. It could stay as BigDecimal but internally store the data as a Rational. Looking at Java BigDecimal it throws an exception if the result of a division cannot be held exactly. In summary however I think you are correct for Ruby. BigDecimal will retain full accuracy except for division, where it is up to the user to make sure he/she understands what he is doing and ensures that errors are understood. Colin > > HTH, > > Peter > > -- > 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 rubyonrails-t...@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.