From: "Peña, Botp" Date: 2007-08-30T16:46:36+09:00 Subject: Re: Bug in % (Float)? From: Morton Goldberg [mailto:m_goldberg@ameritech.net] # On Aug 29, 2007, at 10:14 PM, Pe�a, Botp wrote: # >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] # > irb(main):347:0> 9*0.1+0.1 # > => 1.0 # # 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 indeed, that is the effect you are testing. what i'm saying is that, A. result of q from q,r=x.divmod(y) may _not (always) guarantee that q == (x/y).floor B. there are too many results of that kind above, so inferred this is not plain ruby algo error. see, irb(main):069:0> (0.5/0.1).floor => 5 irb(main):070:0> 0.5.divmod(0.1) => [4, 0.1] irb(main):071:0> (0.6/0.1).floor => 5 irb(main):072:0> 0.6.divmod(0.1) => [5, 0.1] irb(main):073:0> (0.7/0.1).floor => 6 irb(main):074:0> 0.7.divmod(0.1) => [6, 0.0999999999999999] irb(main):075:0> (1.2/0.1).floor => 11 irb(main):076:0> 1.2.divmod(0.1) => [11, 0.0999999999999999] irb(main):077:0> (1.3/0.1).floor => 13 irb(main):078:0> 1.3.divmod(0.1) => [12, 0.1] i checked divmod second value is same as modulo result (fr which i based my previous statement to not trust float division; of course, i may be wrong). so i reckon, ruby gets modulo first, then generate q by q=(num-r)/div. Thus ruby can only guarantee num = q*div + r. (maybe modulo is wrong, and the documentation can be wrong too). i think. i'm sorry. i tend to generalize. i don't read ruby source. correct me if i'm wrong, pls. kind regards -botp