Float Comparison Returns False

bearophile bearophileHUGS at lycos.com
Thu Jul 7 17:28:41 PDT 2011


Loopback:

> I do want to ask though what an alternative would be in this case, to
> compare the two different values. You mentioned something about
> "epsilons" but I have no experience within this field. I would really
> appreciate an example or something similar so I could understand your
> statement.

If you want to use floating point values in your programs then you probably need to know something about floating point representation. This is a good starting point:
http://en.wikipedia.org/wiki/Floating_point
For your problem there is the feqrel function:
http://www.digitalmars.com/d/2.0/phobos/std_math.html#feqrel


>  From what I can see these are two identical values, I would be more than
> glad if someone could explain just what is the difference between these
> two "non-equal" values and how make them "equal". Perhaps I should use
> ints and long instead since they don't seem to suffer from this
> "problem"?

Generally in a program you use floating point values only if you can't use integral values (and you don't want to go toward fixed point values, rationals, etc).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list