0

is this a ruby bug or a problem in my system . i'm using ubuntu

 moo@jmj uni (master) $ ruby -v
    ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
    moo@jmj uni (master) $ ^C
    moo@jmj uni (master) $ irb
    2.1.2 :001 > 0.05 +0.01
     => 0.060000000000000005 
    2.1.2 :002 > 
Mohammad
  • 40
  • 7
  • It's neither. This is how floating point arithmetics works. – Marek Lipka Apr 12 '15 at 09:54
  • 1
    It's primarily for Java, but explains the problem in great detail, check: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Paweł Dawczak Apr 12 '15 at 09:56
  • what that means? 0.05 +0.01 => 0.060000000000000005 but 0.03 +0.01 => 0.04 – Mohammad Apr 12 '15 at 09:57
  • You might want to read about [accuracy problems with floating point number](https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems). To avoid problems like this, use [BigDecimal](http://ruby-doc.org/stdlib-2.2.1/libdoc/bigdecimal/rdoc/BigDecimal.html). – spickermann Apr 12 '15 at 12:30

0 Answers0