From: Jason Roelofs Date: 2007-12-05T23:31:26+09:00 Subject: Re: what's wrong with these calculations? ------=_Part_1660_14101384.1196865082053 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Dec 5, 2007 9:15 AM, Phrogz wrote: > On Dec 5, 4:54 am, hemant wrote: > > On Dec 5, 2007 5:22 PM, hemant wrote: > > > > > > > > > On Dec 5, 2007 5:00 PM, Ivo Dancet > wrote: > > > > Hi > > > > > > I thought, why search for two hours being totally frustrated if I > can > > > > just ask... > > > > > > why does this happen: > > > > > > >> 73.07-63.00 > > > > => 10.07 # ok, normal! > > > > >> 73.07-64.00 > > > > => 9.06999999999999 # why oh why? > > > > > Thats folly of floats, you can't do much apart from : > > > > > sprintf("%.2f",(73.07-64.00)).to_f > > > > generally, I add a method to Float class like this: > > > > class Float > > def r2p places > > sprintf("%.#{places}f",self).to_f > > end > > end > > Note that, due to the nature of floats, calling to_f on that string > brings you back into the problem again: > > x = ( 73.07 - 64.00 ) > y = x.r2p( 2 ) > p x, y, "%.20f" % y > > #=> 9.06999999999999 > #=> 9.07 > #=> "9.07000000000000028422" > > require 'bigdecimal' require 'bigdecimal/math' include BigMath (BigDecimal("73.07") - BigDecimal("64.00")).to_f #=> 9.07 http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/index.html Jason ------=_Part_1660_14101384.1196865082053--