Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Equality checking for strings AND numbers

by BrowserUk (Patriarch)
on Jul 13, 2007 at 00:21 UTC ( [id://626344]=note: print w/replies, xml ) Need Help??


in reply to Equality checking for strings AND numbers

If your data can contain reals, you might want to think about whether 10.0 == 10.000000000000001, or not.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Equality checking for strings AND numbers
by Anonymous Monk on Jul 13, 2007 at 00:38 UTC
    Good observation. Most values are integers, but with different precisions. Real numbers SHOULD have the same precisions in these files, and I actually want to detect if they don't i.e. 10 and 10.000000000000001 should be treated as different. If exact comparison on reals becomes and issue, I guess I could use sprintf to compare only the leading decimal places, or do a ratio comparison. Thanks for the heads-up.

      when comparing numbers, I tend to avoid using == to go for something like:

      sub equality{ my ($a, $b, $eps) = @_; abs( $a-$b ) < $eps ? return 1: return 0; }

      where $eps is the desired precision

      Cheers,

      lin0
        lin0's code above is generally referred as the "Within Epsilon" check, and is an important advancement in understanding how to compare floating point numbers. It may be the best approach you can do in pure perl, at least with any hope of runtime performance.

        However, the Within Epsilon family of checks is terribly sensitive to the actual values involved. Properly choosing a threshold epsilon (or the value of $eps in lin0's code) is important and unfortunately, depends on the values you are trying to compare.

        An epsilon of 0.00000000000001 will not be useful for larger numbers in the billions, since the floating point number has to hold a larger exponent and can thus not hold as much precision in the mantissa.

        Anyone who wants to know more about comparing IEEE floating point numbers in software "the right way" should have a brief read through http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm which includes nice example implementations and discussions of useful (and reasonably fast) C "almost equal" checks that will outperform and adapt better than the classic and naive Within Epsilon technique.

        --
        [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-03-29 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found