From: Robert Klemme Date: 2011-04-14T22:28:44+09:00 Subject: Re: Exclusive float range, Range#step result in counterintuitive result On Thu, Apr 14, 2011 at 2:42 PM, Joey Zhou wrote: > Hi everybody, > > I find that: > > if >  range.exclude_end? == true > and >  [begin_obj, end_obj, step].grep(Float) != [] > and >  [begin_obj, end_obj, step].any? {|f| f != f.to_i} > then >  # result will miss the last one > end > > for example: > > p (1...6.3).step.to_a # => [1.0, 2.0, 3.0, 4.0, 5.0], missing 6.0 > p (1.1...6).step.to_a # => [1.1, 2.1, 3.1, 4.1], missing 5.1 > p (1...6).step(1.1).to_a # => [1.0, 2.1, 3.2, 4.300000000000001], > missing 5.4 > > p (1.0...6.6).step(1.9).to_a # => [1.0, 2.9], missing 4.8 > p (1.0...6.7).step(1.9).to_a # => [1.0, 2.9, 4.8], 6.7 == 4.8 + 1.9, > it's ok > > The results seem so counterintuitive, is it a bug? Maybe I should report > it? Hmmm... Float and Range generally don't mix very well. Considering this the results seem at least consistent - if not expected: irb(main):001:0> (1.0...6.6).step(1.9).to_a => [1.0, 2.9] irb(main):002:0> (1.0..6.6).step(1.9).to_a => [1.0, 2.9, 4.8] Line 2 cannot go beyond 4.8 because then it would pass 6.6, so the last value which can really be returned is 4.8. The third dot removes that value from the list. It's at least a tad weird. +0.5 for opening a bug. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/