APM: Perl zero not being zero, but 7e-12

Wayne Walker wwalker at bybent.com
Mon Sep 12 23:38:18 PDT 2005


On Tue, Sep 13, 2005 at 01:23:40AM -0500, tmcd at panix.com wrote:
> On Tue, 13 Sep 2005, Wayne Walker <wwalker at bybent.com> wrote:
> > Number::Tolerant seems to be one way, but YECH.
> 
> Why "YECH"?  It's a library designed for this particular sort of
> problem, and at a cursory glance it looks like it's a reasonable
> implementation.  When you have to start worrying about the fact that
> computers can't represent your numbers exactly, it's an intrinsically
> messy and worrisome problem.
The Yech is that fit's too much overhead for most problems.

Somthine like the folllowing would at least not require creation of
objects for each number while providing much or all of what David needs.  

package Float::AlmostEqual;

use Exporter;
@EXPORT = qw(ae);

our $precision = 0.0000000001;

sub ae
{
	my ( $a, $b ) = @_;
	my $delta = ( $a - $b );
	return ( abs($delta) < $precision );
}

sub re    # reasonably equal
{
	my ( $a, $b ) = @_;
	my $delta = ( $a - $b );
	my $precision;
	my ( $aa, $ab );
	$aa = abs($a);
	$ab = abs($b);
	my $min = min( $aa, $ab );
	$precision = $min / 1000000000;
	return ( abs($delta) < $precision );
}

The above is a vetting the concept, yes I know re() uses an arbitrary
non-pure-math way of deciding on precision, but will work for almost any
problem.  But with it, David could do all his normal porgramming, just
searching for $a == $b and replace with ae($a,$b).  

If he is doing financial, then Math::FixedPrecision or something similar
is the way to go.

-- 

Wayne Walker

wwalker at bybent.com                    Do you use Linux?!
http://www.bybent.com                 Get Counted!  http://counter.li.org/
Perl - http://www.perl.org/           Perl User Groups - http://www.pm.org/
Jabber:  wwalker at jabber.gnumber.com   AIM:     lwwalkerbybent
IRC:     wwalker on freenode.net


More information about the Austin mailing list