in reply to Re^2: find closest element of array without going over
in thread find closest element of array without going over
Except I see at least five errors in your code.
Very interesting indexing into an array with a float. I know perl will DWIM but although perl will round toward 0, I don't think it's guaranteed.my $tmp = ($right+$left) / 2; if ($a[$tmp] > $search_for){ ...
I'm being picky here because the code illustrates that a quick-search is difficult to write correctly. Your code makes my point perfectly, thank you.
Update: Three is a Red Herring. When first I looked at your code I said "Ah, there is the Binary-Search-Overflow bug!" But of course this was a bigger problem when long was the memory space and int was shorter than long. But Perl is written so that an int can access the total memory space and so three is less of a bug that any other operation on large integers would be.
|
---|