Double value comparisons?

  • Thread starter Thread starter Fred W.
  • Start date Start date
F

Fred W.

I want to compare to double values, as follows:

double max = 1000.0;

double y = 1000.0;

if (y>max) throw new ApplicationException("Value y exceeds the max");



'y' could be anything in my actual application. Is this valid? Or does my
comparison need to be:

if (y> (max + double.Epsilon) ) throw ....
 
Fred said:
I want to compare to double values, as follows:

double max = 1000.0;

double y = 1000.0;

if (y>max) throw new ApplicationException("Value y exceeds the max");



'y' could be anything in my actual application. Is this valid? Or
does my comparison need to be:

if (y> (max + double.Epsilon) ) throw ....

In a case like this, you probably don't want to use an episilon. If 1000.0
is truly your upper limit, then compare to that number. The fuzziness of
floating point math will come into play when someone passes in a value like

float f = 1000.0;
f /= 3;

y = f*3;

Is y > 1000 or < 1000? It could go either way depending on how the rounding
was done in the original division. If in this case, y turns out to be
1000.00001, do you want that to trigger your exception? If so, don't use
episilon. On the other hand, if you'd like this case to be accepted, then
add an appropriate small value of episilon.

-cd
 

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

Back
Top