Morton Goldberg
8/30/2007 2:05:00 AM
On Aug 29, 2007, at 8:02 PM, Charlie Lehardy wrote:
> Modulo should be a fairly simple operation, however I'm finding
> some very
> odd results and am wondering what is going on... For instance:
>
> irb(main):001:0> 1 % 0.1
> => 0.1
I think you've found a bug. Defining a modulo operator for floats is
a little tricky. Consider
n, d = 1.0, 0.1
q, r = 1.0.divmod(0.1) # => [9.0, 0.1]
n == q * d + r # => true
So the answer you're getting satisfies the basic invariant imposed by
division. But it should also satisfy the additional constraint that
r.abs be minimal, and it fails there.
Regards, Morton