Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 ]


In reply to Equality checking - Comparing floats by halley
in thread Equality checking for strings AND numbers by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-23 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found