From: Roger Pack Date: 2009-06-14T05:10:36+09:00 Subject: Re: a few thoughts for ruby... > Doing a quick bit of testing suggested that using "%f" % float > might be better than using float.to_s because in some cases > the former preserves information that is actually in the float > which the latter uses. In fact, I was going to suggest you propose > to use "%f" % self in Float#to_d instead of self.to_s. > But after some more thought and a bit of testing, I found that > for me in IRB "%f" % float only gives results to 6 decimal places > but float.to_s seems to give results to 14 or 15 significant figures > so overall using float.to_s for the conversion seems better. The good news is that with current ruby trunk I believe that Float#to_s is as accurate as doing a ("%f" % self) [it no longer loses accuracy]. > If we want a BigDecimal method (and in view of BigDecimal( float.to_s ) > I don't think we do - but BigDecimal( float.to_s ) should perhaps > be mentioned prominently in BigDecimal documentation) > how about something like (not tested): > def BigDecimal.from_f( f ) > if f.kind_of?( Float ) then BigDecimal( f.to_s ) > else raise "BigDecimal.from_f: argument must be a Float" > end > end That would be nice, though the way you suggested earlier (require 'bigdecimal/util'; 3.3.to_d) works for me :) -= -- Posted via http://www.ruby-forum.com/.