执行计算,但结果只得到NaN
本文关键字:NaN 结果 计算 执行 | 更新日期: 2023-09-27 18:01:20
public double calculateStdErrorEst()
{
double see = 0;
double residualSquare = 0;
CultureInfo culture = new CultureInfo("en-US");
List<double> residualTotal = new List<double>();
try
{
for (int i = 0; i < stockPctReturns.Count; i++)
{
// square root of all sums of predicted - actual or market - stock
residualSquare = Math.Sqrt(Convert.ToDouble((marketPctReturns.ElementAt(i) - stockPctReturns.ElementAt(i)), culture));
residualTotal.Add(Math.Round(residualSquare, 2, MidpointRounding.AwayFromZero));
}
see = residualTotal.Sum() / (stockPctReturns.Count - 2);
see = Math.Sqrt(see);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return Math.Round(see, 2, MidpointRounding.AwayFromZero);
}
正如你所看到的,我试图将值四舍五入,这样我就可以保持数字小,但无论我尝试过什么,我一直得到NaN
作为唯一的结果。我遗漏了什么吗?
您可能试图从负值中获得平方根,这导致Math.Sqrt
返回double.NaN
。在调用Math.Sqrt
之前使用Math.Abs
:
residualSquare = Math.Sqrt(Math.Abs(Convert.ToDouble((marketPctReturns.ElementAt(i) - stockPctReturns.ElementAt(i)), culture)));