https://bugs.documentfoundation.org/show_bug.cgi?id=168064
--- Comment #7 from Mike Kaganski <[email protected]> --- (In reply to Olivier Hallot from comment #6) Thanks Olivier! > If Type = 1 or the third parameter is missing, the function returns the > index of the last value encountered that is smaller than or equal to the > search criterion. > > The search stops at this match and the remaining values of the lookup array > is ignored. > > To obtain the index of the highest value that is smaller than or equal to > the search criterion, the search array must be sorted in ascending order. Technically you are indeed correct. The binary search algorithm does that: as soon as it encounters an adjacent pair of values, one of which is lower (or equal) than the key, and one higher, it stops and returns the lower one. However, in case of unsorted array, this text will likely be too precise, to the degree that it will mislead people. E.g., people might imagine, that it will find *the first one in the array*, when in reality, it will be the first one found by bisection, i.e. by always looking at the center of the examined range; and with that in mind, you can see that the returned value could happen to be ~any in the unsorted array. In principle, we must only explain what happens when it is sorted, and warn that it won't work on unsorted, not to hint on what the result could be in unsorted case. I suggest to avoid the distinction between the "highest" and "last" value that is smaller or equal. Something like "in sorted array, it finds the last value not lower than the search key. If the array is not sorted, the result is undefined" (where "undefined" means "you should not rely on what will be the result", not "the function will return some special "UNDEFINED" value" - no idea how to word that correctly). And of course, the same (modulo the sort direction) for -1. -- You are receiving this mail because: You are the assignee for the bug.
