1

Possible Duplicate:
ruby floating point errors

In Ruby, when subtracting 4.7 from 5.0, I would expect a result of 0.3 but get:

~ $ irb
1.9.2p290 :001 > 5.0 - 4.7
 => 0.2999999999999998 

I'm guessing there is a reason for this rather than it being a bug? Using BigDecimal objects yields the same result. Is my only option to use round on the result?

Community
  • 1
  • 1
james246
  • 1,884
  • 1
  • 15
  • 28
  • Ah, using BigDecimal with a string as the parameter gets me what I want: `(BigDecimal.new('5.0') - BigDecimal('4.7')).to_f`, but not `(BigDecimal.new(5.0, 0) - BigDecimal(4.7, 0)).to_f` – james246 Dec 04 '12 at 15:43

2 Answers2

7

Floats lose precision. Nothing can be done about that. So, use:

(5.0 - 4.7).round(1)

and if you had needed more precision:

(0.50 - 0.47).round(2)
xpda
  • 15,585
  • 8
  • 51
  • 82
Candide
  • 30,469
  • 8
  • 53
  • 60
2

It's not a bug. It's how floating point arithmetics works. More details here: http://floating-point-gui.de/

DNNX
  • 6,215
  • 2
  • 27
  • 33
  • Thanks for the link, I knew it was _something_ to do with how Float works, but not exactly why. Will give it a read. – james246 Dec 04 '12 at 15:56