From: Morton Goldberg Date: 2007-08-30T14:28:11+09:00 Subject: Re: Bug in % (Float)? On Aug 29, 2007, at 10:14 PM, Pe�a, Botp wrote: > From: Charlie Lehardy [mailto:charlie.lehardy@gmail.com] : > # irb(main):001:0> 1 % 0.1 > # => 0.1 > # > # Shouldn't 1 % 0.1 be 0.0 and not 0.1? > > never trust floats in division :) > ruby tries to be friendly at the expense of more surprises... > > irb(main):343:0> system "qri numeric.divmod | head -7" > --------------------------------------------------------- > Numeric#divmod > num.divmod( aNumeric ) -> anArray > ---------------------------------------------------------------------- > -- > Returns an array containing the quotient and modulus obtained by > dividing num by aNumeric. If q, r = x.divmod(y), then > > q = floor(float(x)/float(y) > > irb(main):345:0> 1.divmod 0.1 > => [9, 0.1] If Numeric#divmod adhered to the above, the result would be [10.0, 0.0], so the result is just plain wrong, not a matter of float inaccuracy. Proof: x, y = 1.0, 0.1 (x/y).floor.to_f # => 10.0 > irb(main):347:0> 9*0.1+0.1 > => 1.0 Regards, Morton