From: Brian Candler Date: 2010-07-12T19:15:36+09:00 Subject: Re: ruby float modulus operation. Karthick S. wrote: > I am new to ruby and found this while experimenting with ruby. > > irb(main):010:0> 5.1%0.5 > => 0.0999999999999996 Ruby is no different to any other language which uses binary floating point arithmetic: $ perl -e 'print 5.1 - 10*0.5, "\n"' 0.0999999999999996 The problem is that 1/10 does not have an exact representation in binary floating point, in the same way that 1/3 does not have an exact representation in decimal floating point (0.33333...) To learn more see http://docs.sun.com/source/806-3568/ncg_goldberg.html For some applications, you might use the Rational or BigDecimal classes instead. If dealing with currency, you can work in Integers for cents/pence etc. Or maybe you just want to use fewer significant digits when printing: >> v = 5.1 % 0.5 => 0.0999999999999996 >> "%.8f" % v => "0.10000000" although you're still likely to be tripped up by this: >> v == 0.1 => false HTH, Brian. -- Posted via http://www.ruby-forum.com/.