What is the minimal step in double data type? (.NET)

谁说胖子不能爱 提交于 2019-12-24 12:15:47

问题


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.


回答1:


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).




回答2:


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



来源:https://stackoverflow.com/questions/15478774/what-is-the-minimal-step-in-double-data-type-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!