Math.Round()实际上并没有使我的双打四舍五入

时间:2014-06-05 19:24:42

标签: c# ieee-754

我试图返回一个四舍五入到两位小数的double。我已经介入并验证了,在每一步中,我的所有价值都是doubles。这是我的方法:

public double FindAzimuthAtPointK(LwdData pointKData, SurveyData stationOne, SurveyData stationTwo)
{
    var deltaDepth = stationTwo.MeasuredDepth - stationOne.MeasuredDepth;

    var valueG = FindValueG(pointKData, stationOne, stationTwo);
    var valueH = FindValueH(pointKData, stationOne, stationTwo);
    var valueY = FindValueY(stationOne, stationTwo);

    var sinStationOneInclination = Math.Sin(RadianToDegreeConversion * stationOne.Inclination);
    var sinStationTwoInclination = Math.Sin(RadianToDegreeConversion * stationTwo.Inclination);

    var sinStationOneInclinationSquared = Math.Pow(Math.Sin(RadianToDegreeConversion * stationOne.Inclination), 2);
    var sinStationTwoInclinationSquared = Math.Pow(Math.Sin(RadianToDegreeConversion * stationTwo.Inclination), 2);

    var partOneBottom = ((180 * deltaDepth) * Math.Tan(RadianToDegreeConversion * (valueG*valueY) / 2)) / (Math.PI * valueY);

    var partOneTotal = Math.Pow((valueH / partOneBottom), 2);

    var partTwo = sinStationOneInclinationSquared * sinStationTwoInclinationSquared;

    var topTotal = RadianToDegreeConversion * (partOneTotal - partTwo);

    var bottom = 2 * sinStationOneInclination * sinStationTwoInclination;

    var acosValue = Math.Acos(topTotal / bottom);

    var azimuthAtPointK = stationOne.Azimuth + acosValue;

    return Math.Round(azimuthAtPointK, 2);
}

这里是客户手写的实际数学方程(和子方程): enter image description here enter image description here

现在,这里实际发生的是返回值是315.57999999999998,它应该是315.58。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:5)

所有关于双精度的精确度和它们可以准确表示的数字。如果您需要绝对精确度,请使用小数,或者在结尾处舍入结果以用于显示目的。

要回答有关最后四舍五入的问题,可能是因为315.58的结果无法准确显示为2到2的两倍。