From: Rick DeNatale Date: 2010-05-12T00:30:08+09:00 Subject: Re: ruby 1.9+ , floats, and decimal On Tue, May 11, 2010 at 11:09 AM, Roger Pack wrote: > >> 1). Just because BigDecimal has what seems like better behavior in >> some cases compared to a binary float, it has just as many problems in >> other cases. >> >>    (BigDecimal("1") / BigDecimal("3")).to_s >>  => "0.333333333333333333333333333333333333E0" >> >>    Both binary and decimal floats have an infinite number of real >> numbers which they can't express exactly. > > Good point. > Maybe a good default would be Rational then? > >>> Rational(1,3) > => (1/3) No there is no universally good default. The best representation depends on the problem, that's just something that programmers will ultimately need to face. > > >> And extended example which might make the point a bit better: >> ((BigDecimal("1") / BigDecimal("3"))*BigDecimal("3")).to_s >> => "0.999999999999999999999999999999999999E0" > > That one seemed to work ok.  Was your point that it is limited to 36 > decimals by default? So you don't expect (1/3) * 3 to equal 1? You can't represent 1/3 exactly as a float in base 10, as long as you have a finite number of digits, and you can't have more digits than you have memory to hold. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale