From: "marcandre (Marc-Andre Lafortune)" Date: 2013-01-22T18:48:50+09:00 Subject: [ruby-core:51563] [ruby-trunk - Bug #7724][Open] 6 bugs with Range#bsearch over floats Issue #7724 has been reported by marcandre (Marc-Andre Lafortune). ---------------------------------------- Bug #7724: 6 bugs with Range#bsearch over floats https://bugs.ruby-lang.org/issues/7724 Author: marcandre (Marc-Andre Lafortune) Status: Open Priority: Normal Assignee: Category: core Target version: 2.0.0 ruby -v: r38825 Take the following code, with from, to and search all Integer or all Float: (from...to).bsearch{|f| f >= search} I expected it to: 0) never yield and return nil if range is empty, i.e. from >= to ("trivial test") 1) never yield more than 65 times for floats or Math.log(to-from, 2)+1 for Integer ("log test") 2) return search if from <= search < to, nil otherwise ("coverage test") 3) never yield the same `f` ("uniqueness test") 4) if range is exclusive, never yield `to` nor return `to`; if range is inclusive and block returns false always yield `to` ("end of range test") 5) never yield a number < from or > to ("out of range test") These conditions are all respected for Integers but all can be broken for Floats Test 0, 1, 3, 4 & 5 fail even for small ordinary float values, while test 2 requires some larger floats, say 1e100 to fail. For example bsearch can yield over 2000 times (instead of at most 65). These tests and a correct Ruby implementation of bsearch can be found here: https://github.com/marcandre/backports/compare/marcandre:master...marcandre:bsearch My implementation also passes the MRI tests. -- http://bugs.ruby-lang.org/