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:
-
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?
-
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.)
-
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.)
| [reply] |
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.
| [reply] |