From: Cameron McBride Date: 2006-03-10T02:06:26+09:00 Subject: Re: ruby float calculations On 3/9/06, Alexandru Toma wrote: > Could someone please explain to me why the following happens: > > irb(main):001:0> 0.29 - 0.38 + 0.1 > => 0.00999999999999998 > irb(main):002:0> 0.29 + (-0.38 + 0.1) > => 0.00999999999999995 > irb(main):003:0> 0.29 + 0.1 - 0.38 > => 0.01 Check this out: d1 = 0.29 - 0.38 + 0.1 d2 = 0.29 + (-0.38 + 0.1) d3 = 0.29 + 0.1 - 0.38 p (d3 - d2) p Float::EPSILON (d3 - d2) < Float::EPSILON #=> true Computers do not store numbers indefinitely, so there is an "accuracy" associated with floating point operations. For more information, check out: http://en.wikipedia.org/wiki/Floating_point Cameron