Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Seeking Algorithm

by tkil (Monk)
on May 28, 2004 at 15:55 UTC ( #357270=note: print w/replies, xml ) Need Help??


in reply to Re: Seeking Algorithm
in thread Seeking Algorithm

There's no need for an "approx_equal" to do fancy floating point compare. You get the numbers from a split - so you have strings. Just use string compare and avoid the uncertainy problems you have with floating point compare.

At one point in the evolution of my response, I was relying on strings to avoid this problem. Three reasons I switched:

  1. The original post didn't make it clear that all numbers would have exactly one decimal point. If the row contained 305.10, did that count? How about 305.099?
  2. I can never remember if scalars keep both their string and numeric natures at the same time. After using a numeric comparison against a given scalar, is it now just a number, or are both kept around? (I could find out by research or experimentation, but the fact that I had to think about it, even after 10+ years of using Perl, makes me think that I should avoid this subtlety.)
  3. Finally, it was a way to throw in an educational tidbit "for free". The original author didn't specify the example very precisely; by including this in my response, it would hopefully help them think about it more clearly. (And/or I was showing off. You decide.)

Replies are listed 'Best First'.
Re: Re^2: Seeking Algorithm
by WhiteBird (Hermit) on May 28, 2004 at 17:30 UTC
    For clarification, in my case there will never be more than two decimal points and the data is very precise in that there might be a 305.1 or a 310.10 but there would never be a 305.10 or 310.1 either. While I didn't need the complexity of floating decimals, someone in the future might have a similar-but-different problem where this will be important. The "free tidbit" is always a plus.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2022-07-03 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (103 votes). Check out past polls.

    Notices?