in reply to Re: eq vs ==
in thread eq vs ==

i would say that the correct way to compare use == with floating numbers is to not use floating numbers.

Update: as moritz said, i've wrongly used "compare" instead of "use =="

Replies are listed 'Best First'.
Re^3: eq vs ==
by moritz (Cardinal) on Nov 11, 2009 at 10:23 UTC
    Would you care to explain how you would do that, in the general case?

    For example in one of my programs I have a few matrices in which I put parameters, solve some equation systems and in the end I calculate a trace over some of these matrices, and as a result I get another matrix. In that final matrix the sum over all rows and all columns should be equal, modulo numeric errors.

    How would I compare (or even compute) these sums without using or comparing floating point numbers?

    Perl 6 - links to (nearly) everything that is Perl 6.
Re^3: eq vs ==
by oha (Friar) on Nov 11, 2009 at 11:01 UTC
    not using float or not compare ==.

    (or using abs($x-$y) < $epsilon as you already shown which is not the compare anyway)

    I know my position is a bit strong, but using == on floats is going to make troubles, which depends on values, host, compiler, libc and so on. very hard to predict and replicate.

    maybe a nice gift would be to have a warning?

    Update: ops! i've replied to my node instead of moritz's 806454.