Friday, April 29, 2011

double.Epsilon vs. std::numeric_limits<double>::min()

Why double.Epsilon != std::numeric_limits<double>::min()?

On my PC: double.Epsilon == 4.9406564584124654E-324 and is defined in .NET std::numeric_limits<double>::min() == 2.2250738585072014e-308

Is there a way to get 2.2250738585072014e-308 from .NET?

From stackoverflow
  • Well you could use C++/CLI to return the value:

    double epsilon() { return std::numeric_limits<double>::min(); }
    

    Why would you want to though? Why do they have to be the same? You should try to avoid skating on the edges of your floating point numbers.

  • Epsilon is the minimal possible difference between two doubles. (Edit: not exact, it's the minimal positive nonzero number in this case).

    double.MinValue
    

    is what you need.

    Jon Skeet : The OP is looking for a tiny but positive number. double.MinValue is a massive and negative number.
    Yossarian : Uh? I thought, tnat numeric_limits returns minimal value that can be represented, not minimal nonzero number..
    Jon Skeet : Take a look at the values given in the question: "std::numeric_limits::min() == 2.2250738585072014e-308"
  • They're different because double.Epsilon returns the smallest representable value. numeric_limits<double>::min() returns the smallest normalized value.

    Basically double.Epsilon is the equivalent to numeric_limits<double>::denorm_min().

    The easiest way of getting the equivalent in .NET is probably to work out the bit pattern for the minimal normalized number and use BitConverter.Int64BitsToDouble.

    sthiers : Exactly the kind of answer I was looking for. Thanks Jon

0 comments:

Post a Comment