using System.Collections.Generic;
private static double Interpolate(List<(double x, double y)> list, double x) {
var index = list.BinarySearch((x, 0),
Comparer<(double x, double y)>.Create((left, right) => left.x.CompareTo(right.x)));
int leftIndex = ~index - 1;
int rightIndex = leftIndex + 1;
if (rightIndex == list.Count) {
double xa = list[leftIndex].x;
double ya = list[leftIndex].y;
double xb = list[rightIndex].x;
double yb = list[rightIndex].y;
return ya + (x - xa) / (xb - xa) * (yb - ya);
public static void Main() {
var data = new List<(double x, double y)> {
var result = Interpolate(data, 1.5);
Console.WriteLine(result);