我在哪里可以找到C#中的机器epsilon



机器epsilon被规范定义为最小的数字,它加在一上,得到不同于一的结果。

有一个Double.Epsilon,但这个名称非常误导:它是可表示的最小(非规范化的)Double值,因此对任何类型的数值编程都没有用处。

我想获得Double类型的trueε,这样就不必在程序中硬编码公差。我该怎么做?

它(在我的机器上):

   1.11022302462516E-16

你可以很容易地计算它:

        double machEps = 1.0d;
        do {
           machEps /= 2.0d;
        }
        while ((double)(1.0 + machEps) != 1.0);
        Console.WriteLine( "Calculated machine epsilon: " + machEps );

编辑:

我计算了2次ε,现在应该是正确的。

Math.NET库定义了一个Precision类,该类具有DoubleMachineEpsilon属性。

你可以看看他们是怎么做的。

根据它是:

    /// <summary>
    /// The base number for binary values
    /// </summary>
    private const int BinaryBaseNumber = 2;
    /// <summary>
    /// The number of binary digits used to represent the binary number for a double precision floating
    /// point value. i.e. there are this many digits used to represent the
    /// actual number, where in a number as: 0.134556 * 10^5 the digits are 0.134556 and the exponent is 5.
    /// </summary>
    private const int DoublePrecision = 53;
    private static readonly double doubleMachinePrecision = Math.Pow(BinaryBaseNumber, -DoublePrecision);

因此,根据这个来源,它是1,11022302462516E-16

只需对值进行硬编码:

const double e1 = 2.2204460492503131e-16;

或者使用两种力量:

static readonly double e2 = Math.Pow(2, -52);

或者使用你的定义(或多或少):

static readonly double e3 = BitConverter.Int64BitsToDouble(BitConverter.DoubleToInt64Bits(1.0) + 1L) - 1.0;

请参阅维基百科:机器epsilon。

LAPACK+DLAMCH,64位INTEL处理器,C#:

var pp = double.Epsilon; // pp = 4.94065645841247E-324
double p = NativeMethods.MachinePrecision('S'); // =DLAMCH('S') 
p = 2.2250738585072014E-308
double.MinValue = -1.7976931348623157E+308
double.MaxValue =  1.7976931348623157E+308

Ref。Meonester的例行程序:事实上,machEps在退出do。。。而循环是这样的,1+machEps==1。要获得机器epsilon,我们必须返回到上一个值,在循环后添加以下内容:machEps*=2.0D;这将返回2.2204460492503131e-16,符合微软Double.Epsilon.

文档中的建议

最新更新