Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
827 views
in Technique[技术] by (71.8m points)

language agnostic - Is the use of machine epsilon appropriate for floating-point equality tests?

This is a follow-up to Testing for floating-point value equality: Is there a standard name for the “precision” constant?.
There is a very similar question Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to.


It is well known that an equality test for two floating-point values x and y should look more like this (rather than a straightforward =):

abs( x - y ) < epsilon   ,   where epsilon is some very small value.

How to choose a value for epsilon?

It would obviously be preferable to choose for epsilon as small a value as possible, to get the highest-possible precision for the equality check.

As an example, the .NET framework offers a constant System.Double.Epsilon (= 4.94066 × 10-324), which represents the smallest positive System.Double value that is greater than zero.

However, it turns out that this particular value can't be reliably used as epsilon, since:

0  + System.Double.Epsilon ≠  0

1  + System.Double.Epsilon =  1   (!)

which is, if I understand correctly, because that constant is less than machine epsilon.

→ Is this correct?

→ Does this also mean that I can reliably use epsilon := machine epsilon for equality tests?

Removed these two questions, as they are already adequately answered by the second SO question linked-to above.


The linked-to Wikipedia article says that for 64-bit floating-point numbers (ie. the double type in many languages), machine epsilon is equal to:

2-53,   or approx. 0.000000000000000111 (a number with 15 zeroes after the decimal point)

→ Does it follow from this that all 64-bit floating point values are guaranteed to be accurate to 14 (if not 15) digits?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How to choose a value for epsilon?

Short Answer: You take a small value which fits your applications needs.

Long Answer: Nobody can know which calculations your application does and how accurate you expect your results to be. Since rounding errors sum up machine epsilon will be almost all times far too big so you have to chose your own value. Depending on your needs, 0.01 be be sufficient, or maybe 0.00000000000001 or less will.

The question is, do you really want/need to do equality tests on floating point values? Maybe you should redesign your algorithms.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...