Comparing doubles

D

David Veeneman

What's the best way to compare doubles? I am testing a method that should
return the fraction 2/9. For my expected result, I entered 0.222222 (a six
decimal place representation of 2/9). The CLR resolved the fraction to full
precision, so I got this error back from my test:

expected:<0.222222>
but was:<0.222222222222222>

For my purposes, the two values are equal.

The Double.Epsilon constant is too small to use for an epsilon comparison
here. So, is my only option to create my own epsilon constant as 0.00001? Or
is there another solution to this problem that I am missing? Thanks in
advance

David Veeneman
Foresight Systems
 
M

Michael A. Covington

Test whether they are equal to within 0.001 or whatever you consider the
tolerance to be. That's the only way.

Remember that they could differ in either direction, so test the absolute
value of the difference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top