Reputation: 615
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.
Upvotes: 1
Views: 279
Reputation: 11111
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#
Edit:
Implementation in C# see here
next higher/lower IEEE double precision number
Upvotes: 4
Reputation: 50313
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).
Upvotes: 4