简体   繁体   English

RemoveNoiseFromDoubleMath有什么作用?

[英]What does RemoveNoiseFromDoubleMath do?

In the Silverlight Toolkit there is this method: 在Silverlight工具包中,有以下方法:

    /// <summary>
    /// Removes the noise from double math.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <returns>A double without a noise.</returns>
    internal static double RemoveNoiseFromDoubleMath(double value)
    {
        if (value == 0.0 || Math.Abs((Math.Log10(Math.Abs(value)))) < 27)
        {
            return (double)((decimal)value);
        }
        return Double.Parse(value.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);
    }

I don't understand what the purpose is, is there noise in doubles from doing math in .Net? 我不明白目的是什么,在.Net中进行数学运算是否会产生两倍的噪音?

What is with the cast double to decimal to double? 将double转换为十进制到double是什么?

Why do a ToString the Parse? 为什么要使用ToString进行解析?

I wrote a quick test project to generate some random numbers and run it through the method and I didn't see any changes (not that I thought that I would). 我编写了一个快速测试项目,以生成一些随机数并通过该方法运行它,但是我没有看到任何更改(不是我以为可以)。

What this tries to do is to mitigate the rounding difference inherit to floating point arithmetic. 这样做的目的是减轻舍入到浮点算法的舍入差异。

If you for example take the following snippet: 例如,如果您使用以下代码段:

double result = 1.0 / 7.0;

double difference = result - RemoveNoiseFromDoubleMath(result);

Console.WriteLine(difference);

This produces a difference of -1,38777878078145E-16. 这产生了-1,38777878078145E-16的差。

It's really awesome that this function has a comment block that actually doesn't explain what this "noise" it's trying to deal with actually is, and how it affects the value. 真棒,这个函数有一个注释块,它实际上并不能解释它实际上要处理的“噪声”是什么,以及它如何影响值。

The way I read it, if the number has less than 27 base-10 digits (or is exactly zero), it gets converted to a decimal, then back to double. 我的阅读方式是,如果数字的位数少于27个10位数字(或恰好为零),则将其转换为十进制,然后再转换为双精度。 I would have expected the conversion to decimal should have no effect on the value, since decimal has greater resolution than double. 我本来希望转换为十进制应该对值没有影响,因为十进制的分辨率大于double。 Converting back to double should again be lossless. 转换回两倍应该再次是无损的。

Pieter's example shows that this operation actually does appear to affect the value, so apparently conversions from double to decimal and back aren't value-preserving, even when they could be. Pieter的示例显示此操作实际上确实会影响该值,因此,从双精度到十进制再向后的转换显然并不能保持值,即使有可能。 Looking into msdn, I see this: http://msdn.microsoft.com/en-us/library/yht2cx7b(v=VS.80).aspx 调查msdn,我看到这样的情况: http : //msdn.microsoft.com/zh-cn/library/yht2cx7b( v=VS.80) .aspx

which says: 其中说:

When you convert float or double to decimal, the source value is converted to decimal representation and rounded to the nearest number after the 28th decimal place if required. 将float或double转换为十进制时,源值将转换为十进制表示形式,并根据需要四舍五入为小数点后28位之后的最接近的数字。

Okay, so it rounds. 好吧,它四舍五入。 This means that if you have a double with a value of 1.9999999999999999999999999999, the conversion to decimal will round it up to 2, so when it gets converted back to a double, you get a double with a value of exactly 2.0. 这意味着,如果您有一个值为1.99999999999999999999999999999999的双精度数,则转换为十进制会将其舍入为2,因此当将其转换回双精度数时,您将得到一个正好为2.0的双精度数。

If the number has more than 27 digits , it gets converted to a string, then parsed. 如果数字的位数超过27位 ,则将其转换为字符串,然后进行解析。 I'm not sure what the end result of that is - it would depend on the default behavior of ToString and Double.parse. 我不确定这样做的最终结果是什么-它取决于ToString和Double.parse的默认行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM