From: Marc-Andre Lafortune <ruby-core@...> Date: 2011-09-17T00:06:18+09:00 Subject: [ruby-core:39588] [Ruby 1.9 - Bug #4576][Open] Range#step miss the last value, if end-exclusive and has float number Issue #4576 has been updated by Marc-Andre Lafortune. Status changed from Closed to Open Hi, Michal Suchanek wrote: > == is meaningless with floats. > > I am quite sure that the 128.4 are not the same. If `foo != bar`, it doesn't tell you all that much about `foo` and `bar`, as they could differ by a lot, or possibly by very little due to a calculation rounding. But `foo == bar` is completely *meaningful* for two Floats. It means they are the exact same value, that the 64 bits are the same. There are no two 128.4 that are somehow the same but somehow different. This bug is due on some platforms that compare a Float (i.e. double precision 64 bits) with an extended double register (80 bits) which later gets rounded down to a Float. ---------------------------------------- Bug #4576: Range#step miss the last value, if end-exclusive and has float number http://redmine.ruby-lang.org/issues/4576 Author: Joey Zhou Status: Open Priority: Normal Assignee: Category: core Target version: 1.9.4 ruby -v: - =begin Hi, I find that: * if: range.exclude_end? == true * and: any one in [begin_obj, end_obj, step] is a true Float(f.to_i != f) * and: unless begin_obj + step*int == end_obj * then: the result will miss the last value. for example: p (1...6.3).step.to_a # => [1.0, 2.0, 3.0, 4.0, 5.0], no 6.0 p (1.1...6).step.to_a # => [1.1, 2.1, 3.1, 4.1], no 5.1 p (1...6).step(1.1).to_a # => [1.0, 2.1, 3.2, 4.300000000000001], no 5.4 p (1.0...6.6).step(1.9).to_a # => [1.0, 2.9], no 4.8 p (1.0...6.7).step(1.9).to_a # => [1.0, 2.9, 4.8] p (1.0...6.8).step(1.9).to_a # => [1.0, 2.9, 4.8], no 6.7 Maybe the #step is ok on integers, but there's something wrong if the range is end-exclusive and contain float numbers. =end -- http://redmine.ruby-lang.org