Math-Erf-Approx

 view release on metacpan or  search on metacpan

lib/Math/Erf/Approx.pm  view on Meta::CPAN

	my @cases = $class->_test_cases;
	
	plan tests => (2 * @cases);
	
	foreach my $tc (@cases)
	{
		my ($x, $erf, $erfc) = @$tc;
		ok _close_enough(erf($x), $erf), "erf($x)";
		ok _close_enough(erfc($x), $erfc), "erfc($x)";
	}
}

caller(0) or __PACKAGE__->run_tests;
__END__

=head1 NAME

Math::Erf::Approx - pure Perl approximate implementation of the error function

=head1 DESCRIPTION

This is a pure Perl implementation of the error function (a.k.a. the Gauss
error function). It gives an approximation with a maximum absolute difference
of 0.0005 from the real value.

=head2 Functions

This module can export two functions. Neither is exported by default.
This module uses L<Sub::Exporter>, so the functions can be renamed:

 use Math::Erf::Approx -all => { -prefix => 'math_' };

=over

=item C<< erf($x) >>

Calculates the result of the error function for value $x.

=item C<< erfc($x, \&erf) >>

Given a value $x and a code reference to an implementation of erf(),
calculates the complement.

If the code reference is ommitted (which I'd expect would be the most
usual case), then the default is the C<erf> function provided by this
module.

=back

=head2 Testing

It is possible to run a small test suite on this module using:

 use Math::Erf::Approx;
 Math::Erf::Approx->run_tests;
 
=head1 BENCHMARKS

Benchmarking against L<Games::Go::Erf> (on a fairly underpowered netbook)...

 Benchmark: timing 100000 iterations of GGE, MEA...
   GGE:  6 wallclock secs ( 6.34 usr +  0.01 sys =  6.35 CPU) @ 15748.03/s (n=100000)
   MEA:  3 wallclock secs ( 2.71 usr +  0.00 sys =  2.71 CPU) @ 36900.37/s (n=100000)

There are considerations other than raw speed though...

=over

=item * Games::Go::Erf provides much more accurate results

=item * ... and it can calculate inverses

=item * ... B<but> it has a dependency on L<Tk>

=item * ... B<and> it sets C<< $[ >> to 1, which has been deprecated since Perl 5.12.

=back

=head1 SEE ALSO

L<http://en.wikipedia.org/wiki/Error_function>.

I<Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables>,
ed Milton Abramowitz and Irene Stegun. ISBN 0-486-61272-4.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2012 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

=begin trustme

=item run_tests

=end trustme



( run in 0.640 second using v1.01-cache-2.11-cpan-71847e10f99 )