[rspec-users] Problem Comparing Floats

2009-08-24 Thread Alex Chaffee
Check out the following gist: http://gist.github.com/173975 It's from a Test-Driven Intro To Ruby class I'm working on. Looks good, right? Not so fast. Check out this spec: it converts body temperature do t = @temperature.ctof(37) (t*10).round.should == 986 end Why do we

Re: [rspec-users] Problem Comparing Floats

2009-08-24 Thread Alex Chaffee
...and, while I was composing that message, Brian sent me the following: Rspec does provide the be_close matcher. See cheat rspec. @temperature.ctof(37).should be_close(98.6, 0.1) This will work (I'll go update the code now) but it still leaves the problem I mentioned that if you are

Re: [rspec-users] Problem Comparing Floats

2009-08-24 Thread Tom Stuart
On 24 Aug 2009, at 18:56, Alex Chaffee wrote: == will occasionally mysteriously fail. So my proposal remains: can the == matcher do be_close(x, 0.01) for floats? Arguments pro and con? The problem you describe is with Ruby's == operator, not with RSpec. The == matcher must agree with

Re: [rspec-users] Problem Comparing Floats

2009-08-24 Thread David Chelimsky
On Mon, Aug 24, 2009 at 12:56 PM, Alex Chaffeeale...@gmail.com wrote: ...and, while I was composing that message, Brian sent me the following: Rspec does provide the be_close matcher. See cheat rspec. @temperature.ctof(37).should be_close(98.6, 0.1) This will work (I'll go update the code

Re: [rspec-users] Problem Comparing Floats

2009-08-24 Thread Steve Schafer
On Mon, 24 Aug 2009 10:56:00 -0700, you wrote: This will work (I'll go update the code now) but it still leaves the problem I mentioned that if you are unfamiliar with the vagaries of floating point math -- or even if you momentarily forget -- then using == will occasionally mysteriously fail. So

Re: [rspec-users] Problem Comparing Floats

2009-08-24 Thread Alex Chaffee
What about a helpful error message when should == fails on floats: expected 98.6, got 98.6 The expected and actual may appear to be the same due to Ruby's string representation of floating point numbers. For floating point math, we recommend using the be_close() matcher instead. ??? !!!