Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: eq vs ==

by moritz (Cardinal)
on Nov 10, 2009 at 12:35 UTC ( #806207=note: print w/replies, xml ) Need Help??


in reply to eq vs ==

I think the difference that confuses you is that some numbers can be represented by floating points without any error, while others can't. For example 0.1 is an infinite fraction in the binary system.

So while you can safely compare 0.5 * 2.0 == 1.0, you can't safely compare 0.1 * 2.0 == 0.2 (the former is true, the latter false).

The correct way to compare floating point numbers is

if (abs($x-$y) < $epsilon) {...}

Where $epsilon is a small number.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: eq vs ==
by LanX (Sage) on Nov 10, 2009 at 14:38 UTC
    The correct way to compare floating point numbers is ...

    Moritz, in general that's problematic.

    The calculation error doubles with every calculation step, no matter how big epsilon is, you can construct a counterexample, where your rule breaks.

    Consequently you have to limit the numbers of calculations before you can tell which epsilon is save!

    IMHO that's a ridiculous big effort compared to the normal possibility to stay integer right away by multiplicating the fractions away.

    532.2 - 23.2 => 5232 - 232 = 5000 => 500.0

    Cheers Rolf

      The calculation error doubles with every calculation step, no matter how big epsilon is, you can construct a counterexample, where your rule breaks.

      The error only doubles with every step if your algorithm is numerically badly conditioned.

      For usual non-numeric calculations such errors tend to stay very small, unless you do things like substracting two numbers of nearly the same size, and dividing by the result.

      If you do numerics, you should inform yourself in more detail about floating point arithmetics and error propagation (a standard subject in applied mathematics).

      IMHO that's a ridiculous big effort compared to the normal possibility to stay integer right away by multiplicating the fractions away.

      That might be true for literal constants, but when you read several numbers from user input, it gets very quickly very tedious to track the smallest possible multiplier, and outright stops working if you need to divide by a user-supplied number.

      Another solution is to use a data type which stores numerator and denominator separately as integers, as Perl 6 does.

      Perl 6 - links to (nearly) everything that is Perl 6.
        If you do numerics, you should inform yourself in more detail about floating point arithmetics and error propagation (a standard subject in applied mathematics).

        Moritz it's first year computer science...

        Cheers Rolf

Re^2: eq vs ==
by oha (Friar) on Nov 11, 2009 at 09:59 UTC
    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 =="

      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.
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://806207]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-05-27 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (94 votes). Check out past polls.

    Notices?