执行计算但仅获得NaN作为结果

时间:2014-05-02 21:56:31

标签: c#

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作为唯一的结果。我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:4)

您可能尝试从负值获取平方根,这会导致Math.Sqrt返回double.NaN。在致电Math.Abs之前使用Math.Sqrt

residualSquare = Math.Sqrt(Math.Abs(Convert.ToDouble((marketPctReturns.ElementAt(i) - stockPctReturns.ElementAt(i)), culture)));