Double value comparisons?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred W.

    Double value comparisons?

    I want to compare to double values, as follows:

    double max = 1000.0;

    double y = 1000.0;

    if (y>max) throw new ApplicationExce ption("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 ....




  • Carl Daniel [VC++ MVP]

    #2
    Re: Double value comparisons?

    Fred W. wrote:
    I want to compare to double values, as follows:
    >
    double max = 1000.0;
    >
    double y = 1000.0;
    >
    if (y>max) throw new ApplicationExce ption("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


    Comment

    • Kevien Lee

      #3
      Re: Double value comparisons?

      if (max-someValue<y)&&( max-someVale>Y)
      return true;

      the "someValue" is Epsilon

      On Nov 13, 11:32 pm, "Fred W." <fredwemail-n...@yahoo.comw rote:
      I want to compare to double values, as follows:
      >
      double max = 1000.0;
      >
      double y = 1000.0;
      >
      if (y>max) throw new ApplicationExce ption("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 ....

      Comment

      Working...