我有这样的清单
Porfolio Value CumulativeWeights
1246.540179 0.30873%
1254.380054 0.33031%
1258.260394 0.57136%
1262.300797 4.98794%
1262.710843 5.07213%
首先:如果该列表包含第一个值大于5%,请选择该值和
第二:我必须检查列表包含精确的5%选择该值。
如果不满足以上两个条件我们做一些计算。所以如何选择。
ConfidenceValue为5%
private static double CaluculateInterpolation(List<AgeWeightedHistorical> Weights, Double ConfidenceValue, CalaculationSettings oCalcSettings)
{
double interpolation=0;
var CumulativeVal = Weights.Find(n => n.CumulativeWeights > ConfidenceValue);
if (CumulativeVal.CumulativeWeights >= ConfidenceValue)
{
interpolation = CumulativeVal.ProfolioPrices;
}
else
{
var LowestValues = Weights.Last(n => n.CumulativeWeights <= ConfidenceValue);
if (LowestValues.CumulativeWeights == ConfidenceValue)
{
interpolation = CumulativeVal.ProfolioPrices;
}
else
{
var HightValues = Weights.FirstOrDefault(n => n.CumulativeWeights >= ConfidenceValue);
double avgPrices = (LowestValues.ProfolioPrices + HightValues.ProfolioPrices) / 2;
double avgWeights = (LowestValues.CumulativeWeights + HightValues.CumulativeWeights) / 2;
if (avgWeights == ConfidenceValue)
{
interpolation = LowestValues.ProfolioPrices;
}
else
{
double Lowest_val = Math.Abs(LowestValues.CumulativeWeights - ConfidenceValue);
double Higest_val = Math.Abs(HightValues.CumulativeWeights - ConfidenceValue);
var FinalWeight = (Lowest_val < Higest_val) ? LowestValues : HightValues;
double minWeight = Math.Min(avgWeights, FinalWeight.CumulativeWeights) / 100;
double maxWeight = Math.Max(avgWeights, FinalWeight.CumulativeWeights) / 100;
interpolation = avgPrices - ((avgPrices - FinalWeight.ProfolioPrices) * (((1 - (oCalcSettings.PercentageLevelOfConfidence / 100)) - minWeight) / (maxWeight - minWeight)));
}
}
}
return interpolation;
}
即时错误请帮帮我
答案 0 :(得分:1)
我建议在C#中进行精确计算,你应该使用
decimal
这将避免许多舍入问题..
将所有双替换为十进制,检查您的算法,它应该有效。