This can be done quite easily with a comparator and a small wrapper around List<T>.BinarySearch:
static double Search(List<KeyValuePair<double, double>> list, double key) {
int index = list.BinarySearch(
new KeyValuePair<double, double>(key, 0),
new Comparer());
if (index >= 0)
return list[index].Value;
index = ~index;
if (index == 0 || index == list.Count)
return 0;
return (list[index - 1].Value + list[index].Value) / 2;
}
class Comparer : IComparer<KeyValuePair<double, double>> {
public int Compare(
KeyValuePair<double, double> x,
KeyValuePair<double, double> y)
{
if (Math.abs(x.Key - y.Key) < TOLERANCE)
return 0;
return x.Key.CompareTo(y.Key);
}
}