8

I'm not even sure where to begin here. Sorry if this is a duplicate, but I don't even know what to search for or what this particular issue is called.

Randomly, and not all that often, a test in my RSpec suite will fail and I'll get an error like this:

expected: 0.69
     got: 0.69 (0.69e0)

(compared using ==)

The RSpec code is comparing two Floats, from two different models, that should be the same value when the spec is done. Is there a way to reproduce this in a command console? I've tried the obvious stuff (below) but I am honestly at a loss. If I rerun the test a dozen times, I can't reproduce the issue.

0.69 == 0.69e0 => true
0.69 == 0.69 => true
6.9e-1 == 0.69 => true
danielricecodes
  • 3,446
  • 21
  • 23

2 Answers2

10

When testing floating point numbers, I recommend you use RSpec's be_within matcher.

Example:

expect(my_float).to be_within(0.01).of(0.69)

You should choose a resolution that makes sense for your use case. (0.001, 0.0001, etc.)

Fito von Zastrow
  • 710
  • 5
  • 13
3

This is a general problem testing floating point numbers. I always convert them to strings for comparison when using RSpec:

expect(float.to_s).to eq '0.69'
infused
  • 24,000
  • 13
  • 68
  • 78
  • Its almost too simple of a solution. I also wonder if I should be using BigDecimal in my tests instead - as to at least stay with a Numeric type. `float.to_d` should also work? – danielricecodes Mar 15 '19 at 16:33
  • I have a lot of BigDecimals in my specs, but always have to call `to_s` on BigDecimal results too. – infused Mar 15 '19 at 21:36