1

My question is a bit theoretical but actually I need it in practice.

Let's assume that we have two double variables X and Y. Y is certain next double number of X (as you already know we can't keep any real number in double because real number are infinite even in 0 to 1 interval).

Does (Y - X) is constant? And if yes what is it's value?

P.S. My question is related to Microsoft.NET Framework.

TIKSN
  • 615
  • 1
  • 6
  • 24
  • Unless precision is fixed if the value is store in x bits then no. But with a fixed precision is the delta fixed? For decimal this ctor implies yes http://msdn.microsoft.com/en-us/library/bb1c1a6x.aspx but if I was sure I would have posted it as an answer not a comment. – paparazzo Mar 18 '13 at 14:51

2 Answers2

4

The minimum difference between two double values in .NET is Double.Epsilon.

As for comparing double values, see the answers to this SO question for useful information (and as the accepted answer says, Decimal should be used instead of Double whenever possible).

Community
  • 1
  • 1
Konamiman
  • 49,681
  • 17
  • 108
  • 138
  • This means that I can perform a loop and get all double values? – TIKSN Mar 18 '13 at 14:14
  • Looping to get all representable double values is as simple as starting at Double.MinValue then calculating the next representable double value (see link in my answer). Once you reach Double.MaxValue you are done with all "normal" values. There are some special values like negative zero and Infinity/NaN that you may or may not want to include in that enumeration of all double values. – Anders Forsgren Mar 18 '13 at 16:44
4

There is no framework method as far as I'm aware for finding the next representable double or float value from a given value in a given interval. The next representable number from a value d is not d + Double.Epsilon. The distance between consecutive doubles is larger for larger double values. Double.Epsilon is the minimum distance between representable doubles, a distance which only occurs for values near zero.

In C or C++ libraries there are often functions for this, e.g. here in the Boost library for C++. I'll see if I can find an implementation for C#

http://www.boost.org/doc/libs/1_39_0/libs/math/doc/sf_and_dist/html/math_toolkit/special/next_float/nextafter.html

Edit:

Implementation in C# see here

next higher/lower IEEE double precision number

Community
  • 1
  • 1
Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77