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
Shouldn’t 1 % 0.1 be 0.0 and not 0.1? Also, look at the following
example:
irb(main):002:0> a = 1
=> 1
irb(main):003:0> b = 0.1
=> 0.1
irb(main):004:0> a += (a % b) if ((a % b) > 0)
=> 1.1
irb(main):005:0> a += (a % b) if ((a % b) > 0)
=> 1.2
irb(main):006:0> a += (a % b) if ((a % b) > 0)
=> 1.3
irb(main):007:0> a += (a % b) if ((a % b) > 0)
=> 1.4
irb(main):008:0> a += (a % b) if ((a % b) > 0)
=> 1.5
irb(main):009:0> a += (a % b) if ((a % b) > 0)
=> 1.59999999999999
Something is seriously messed up here… Of course, if you do the
following:
irb(main):024:0> a = 1
=> 1
irb(main):025:0> b = 0.1
=> 0.1
irb(main):026:0> a += ((a * 100) % (b * 100)) if (((a * 100) % (b *
100)) >
0)
=> nil
irb(main):027:0> a
=> 1
Everything works as expected (at least as I expect it to work). So,
can
anybody tell me what’s going on here? Are things really broken, or am I
just
expecting the wrong results?
Thanks,
Charlie