Where do I find the machine epsilon in C#?

前端 未结 5 1907
暖寄归人
暖寄归人 2020-12-29 08:01

The machine epsilon is canonically defined as the smallest number which added to one, gives a result different from one.

There is a Double.Epsilon but t

5条回答
  •  萌比男神i
    2020-12-29 08:22

    Just hard-code the value:

    const double e1 = 2.2204460492503131e-16;
    

    or use the power of two:

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

    or use your definition (more or less):

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

    And see Wikipedia: machine epsilon.

提交回复
热议问题