More questions stemming from cookbook work... Decimal Comparisons:

The most common recipe around for comparisons is to use sprintf to cut
the decimals to size and then compare strings. Seems ugly.

The non-stringification way to do it is usually along the lines of: 

if (abs($value1 - $value2) < abs($value1 * epsilon))

(From Mastering Algorithms with Perl errata)

I'm wondering though, if C<$value1 == $value2> is always wrong (or
almost always wrong) then should it be smarter and:

a. throw a warning
b. DWIM using overloaded operators (as in reduce precision then compare)
c. throw a warning but have other comparison operators just for this
case to make sure you know what you're doing

I'd vote for b., but I don't know enough about the problem domain to
know if that is safe, and realistically I just want to write the
cookbook entry rather than start a math-geniuses flame war ;-)

Which leads to another question: Are there $value.precision() and
$value.accuracy() methods available for decimals? I'd really rather
not do the string comparison if it can be avoided, maybe it's just the
purist in me saying "leave the numbers be" :-)

Apologies in advance if this is somewhere I missed. I did a lot of searching.

Marcus Adair

Reply via email to