Float Comparison Returns False

Loopback elliott.darfink at gmail.com
Thu Jul 7 19:31:54 PDT 2011


On 2011-07-08 02:28, bearophile wrote:
> 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
I've looked up both approxEqual and feqrel, and the last one seems most
appropriate, though I cannot use this function without issuing any
errors. If I use the following code:

writefln("Equality: %s", feqrel(m, 1.73205f));

(From the previous example)

I receive this error with the code:
Error: function std.math.feqrel!(float).feqrel has no return statement, 
but is expected to return a value of type int
Error: template instance std.math.feqrel!(float) error instantiating


More information about the Digitalmars-d-learn mailing list