From: Morton Goldberg Date: 2007-09-01T02:04:32+09:00 Subject: Re: Bug in % (Float)? On Aug 30, 2007, at 5:56 PM, Calamitas wrote: >> A reasonable definition for a float modulo would be: >> >> def m_mod_n(m, n) >> m - n * m.quo(n) >> end >> >> With this definition >> >> m_mod_n(1.0, 0.1) # => 0.0 > > This returns 0.0 for m_mod_n(1.0, 0.3) too. Is that the intention? My bad. Confused #quo with #div. Should have written def m_mod_n(m, n) m - n * m.div(n) end m_mod_n(1.0, 0.1) # => 0.0 m_mod_n(1.0, 0.2) # => 0.0 m_mod_n(1.0, 0.3) # => 0.1 m_mod_n(1.0, 0.4) # => 0.2 m_mod_n(1.0, 0.5) # => 0.0 m_mod_n(1.0, 0.6) # => 0.4 m_mod_n(1.0, 0.7) # => 0.3 m_mod_n(1.0, 0.8) # => 0.2 m_mod_n(1.0, 0.9) # => 0.1 and tested before posting. > I don't think you can really ask for anything better. The above def suggests I can. I'm not saying it's a plug-in replacement for %, but it shows that a better answer can be obtained for the case in question. > In Ruby 0.1 > really is 0.10000000000000000555 as that is the floating point > number closest to 0.1 ... That's true ... > ... and it doesn't fit 10 whole times in 1.0. This has nothing to do > with the algorithm used. ... but Ruby appears to think otherwise sprintf("%.25f", 1.0/0.1) # => "10.0000000000000000000000000" causing me to conclude something is amiss in the % operator algorithm. Regards, Morton