From: Alex Chaffee Date: 2012-01-26T03:29:04+09:00 Subject: Re: Microrant on Ruy's Math Skills On Tue, Jan 24, 2012 at 11:15 PM, Gavin Sinclair wrote: > On Wed, Jan 25, 2012 at 6:05 AM, Alex Chaffee wrote: >> >> So how to test around this in unit tests? In RSpec, use be_within (née >> be_close) [1]; in Wrong (which works inside many test frameworks), use >> close_to? [2] > > I'm really impressed with that Wrong library; well done! Glad you like it! It would work even better if MRI attached ASTs and/or source code to procs/lambdas/methods, rather than merely source_location. I'm thinking of logging a bug or two about that. > Just throwing this out there: judging float equality based on > _difference_ is incorrect.  You should use _ratio_, or perhaps in some > cases a combination of both. Fascinating! So if we used division-and-proximity-to-1 instead of subtraction-and-proximity-to-0 then we could possibly do away with the tolerance parameter altogether... or at least redefine it. Of course, your argument reverses itself when dealing with very large numbers! Let's say I'm dealing with Time. If I say time a should be close to time b, then I probably want the same default precision (say, 10 seconds) no matter when I'm performing the test, but using ratios will give me quite different tolerances depending on whether my baseline is epoch (0 = 1/1/1970) or Time.now. >> now = Time.now.to_i.to_f; (now/(now+10)) => 0.9999999924671322 >> now = 1.to_f; (now/(now+10)) => 0.09090909090909091 In any case... I will be happy to review your patch! :-) - A -- Alex Chaffee - alex@stinky.com http://alexchaffee.com http://twitter.com/alexch