5

Possible Duplicate:
Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to

I recently discovered that FLT_EPSILON as defined in <cfloat> is not at all the same as Single.Epsilon as defined in the .NET Framework.

  • FLT_EPSILON is defined as the smallest such that 1.0 + epsilon != 1.0.

  • Single.Epsilon is defined as the smallest possible number greater than zero.

Is there a <cfloat>-style epsilon defined somewhere in the .NET Framework? Or if I need to define my own, should that be defined identically in .NET as the value I see in <cfloat>?

Community
  • 1
  • 1
DuckMaestro
  • 15,232
  • 11
  • 67
  • 85
  • 1
    The linked question doesn't seem to have an answer for the specific cfloat constant I'm looking for, though the other question is certainly related. – DuckMaestro Sep 27 '12 at 18:12
  • 1
    There is no such constant in .NET, the linked question shows the alternative. There's otherwise nothing from stopping you to declare it yourself. The math is the same. – Hans Passant Sep 27 '12 at 18:14
  • The other question and answers are very focused on equality comparisons, which I'm not necessarily here. I'm basically porting code which used epsilon to avoid divide-by-zero => infinite. – DuckMaestro Sep 27 '12 at 18:22
  • Yes, that is what an epsilon is good for. Using one to avoid Infinity in a division is a very long shot. Arbitrarily: 1E160 / 1E-160 generates Infinity, 1E148 / 1E-160 does not. High odds that you are doing it wrong, FLT_EPSILON isn't going to be helpful. – Hans Passant Sep 27 '12 at 21:48
  • @HansPassant I think in our case our units worked out conveniently enough to use FLT_EPSILON to avoid infinity. Your point is valid though. – DuckMaestro Sep 29 '12 at 04:15
  • `FLT_EPSILON` is **not** defined as “the smallest such that 1.0 + epsilon != 1.0”. It is defined as the difference between 1.0f and its successor. http://blog.frama-c.com/index.php?post/2013/05/09/FLT_EPSILON – Pascal Cuoq May 13 '13 at 17:56

1 Answers1

3

By all accounts this is not defined anywhere in the .NET Framework.

If you need the identical constant you can define it as float Epsilon = 1.192092896e-07F;.

For a larger discussion about alternatives and usage see: Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to.

Community
  • 1
  • 1
DuckMaestro
  • 15,232
  • 11
  • 67
  • 85