Hello.

I guess there is such a handler somewhere in here, but I haven’t found it. It is a binary search algorithm, tweaked to return the closest match of a number. The table it looks for data in must be sorted, ascending.

It requires the abs function of Satimage.Osax.

```
property vernalEquinoxIdx : {¬
1700.0, ¬
1800.0, ¬
1900.0, ¬
1950.0, ¬
2000.0, ¬
2100.0, ¬
2200.0}
# set mtch to binarySearchForClosestMatch(vernalEquinoxIdx, 1500)
# set mtch to binarySearchForClosestMatch(vernalEquinoxIdx, 1958)
# set mtch to binarySearchForClosestMatch(vernalEquinoxIdx, 2300)
set mtch to binarySearchForClosestMatch(vernalEquinoxIdx, 2000)
on binarySearchForClosestMatch(theList, value)
local l, r, m, decreasing, probe, diff, diff2, oldL
script o
property lst : theList
end script
set {l, r, decreasing, probe} to {1, (count theList), false, 1.0E+16} # Big number
repeat while true
set m to (l + r) div 2
set {diff, diff2} to {abs (value - (item l of o's lst)), abs ((item m of o's lst) - value)}
# Changed operations, thanks to Bazzie Wazzie.
if diff2 < diff then
set {diff, l} to {diff2, m + 1}
else
set r to m - 1
end if
if diff < probe then
if diff2 = diff then
set oldL to m
else
set oldL to l
end if
set {decreasing, probe} to {true, diff}
else if decreasing then
return oldL
end if
end repeat
end binarySearchForClosestMatch
```