您可以进行线性外推,例如:-
public static decimal ExtrapolateFrom(int f, int s, decimal f1, decimal s2, int value)
{
return (s2-f1)/((s-(decimal)f)/(value-(decimal)f))+f1;
}
public static decimal ExtrapolateFrom(List<Tuple<int, decimal>> table, int value)
{
if(table.Count < 2) throw new Exception("Not enough values to extrapolate from");
var result = table.Select((x, i) => new { x, i }).Where(x => x.x.Item1 >= value).Select(x => x.i).ToList();
var index = result.Any()? result.First() : table.Count-1;
if (index < 1) index = 1;
return ExtrapolateFrom(table[index - 1].Item1, table[index].Item1, table[index - 1].Item2,table[index].Item2, value);
}
private static void Main(string[] args)
{
var table = new List<Tuple<int, decimal>> ()
{
new Tuple<int, decimal>(0, 0.0M),
new Tuple<int, decimal>(100, 5.0M),
new Tuple<int, decimal>(200, 6.0M),
new Tuple<int, decimal>(300, 9.0M),
new Tuple<int, decimal>(500, 11.0M),
};
Console.WriteLine(ExtrapolateFrom(table, 50));
Console.WriteLine(ExtrapolateFrom(table, 400));
Console.WriteLine(ExtrapolateFrom(table, 600));
}
ExtrapolateFrom
需要一张桌子的那个: -
- 检查以确保至少有 2 个截止值从
- 找到表中大于您要转换的值的第一个截止值
- 检查我们是否有一个大于表指定的值,在这种情况下使用最后两个截止值
- 如果我们的值小于表指定的值,在这种情况下使用前两个截止值
- 使用两个表格点进行线性外推。