From: Brent Roman Date: 2009-03-07T15:07:33+09:00 Subject: [ruby-core:22713] Re: suggestions for float How about this: # return true if other (Numeric) is approximately equal #eps is chosen such that =~ indicates whether self.to_s == other.to_s #however, this is at least 30 times faster than comparing the results of to_s #if one were to code it in 'C', I suspect it would be >1000 times faster. class Float @@eps = 5e-15 def =~ other (self-other).abs < (@@eps*self).abs end end It works well for me. Test it and see if it behaves as you would expect. f =~ g is similar enough to the standard "approximately equal" math symbol that I hope many people would understand it at first glance. - brent Roger Pack wrote: > >> A variant of the float to string conversion that tries to preserve all >> the >> binary information would certainly be useful, but not as the default to_s >> method. That is, unless you think that: >> >> irb> 2.1 - 3.0 >> -0.8999999999999999 >> >> would not be "surprising"! (well, at least annoying) > > At least users would be able to understand this: >>> 2.1 - 3.0 > => -0.9 >>> (2.1-3.0) == -0.9 > => false > > Since it would become >>> 2.1 - 3.0 > => -0.8999999999999 >>> (2.1-3.0) == -0.9 > => false > > I'd prefer the second--at least I'm reminded as to what's going on and > don't have to constantly pester ruby talk with questions like "is this > normal?" :) > > However, this would seem surprising >>> 0.9 > => 0.90000000000000002 > > It seems slightly more annoying and verbose but more honest. > That being said, as someone above mentioned, the current float default > output is to "prettify" by stripping the ending few bits [I guess]. > Perhaps we should keep this around as a new method name. Like > to_human_string or something [?] > > -=r > > > -- View this message in context: http://www.nabble.com/-ruby-core%3A22325--suggestions-for-float-tp22144150p22384821.html Sent from the ruby-core mailing list archive at Nabble.com.