NAME

Math::Utils - Useful computational or mathematical functions.

SYNOPSIS

use Math::Utils qw(:fortran);    # Functions originated from Fortran

#
# $dist will be negative or positive $offest, depending
# on whether ($from - $to) is positive or negative.
#
my $dist = 2 * copysign($offset, $from - $to);

#
# Base 10 logarithm.
#
my $scale = log10($pagewidth);

or

use Math::Utils qw(:compare);    # Make comparison functions with tolerance.

#
# Floating point comparison function.
#
my $fltcmp = generate_fltmcp(1.0e-7);

if (&$fltcmp($x0, $x1) < 0)
{
    add_left($data);
}
else
{
    add_right($data);
}

#
# Or we can create single-operation comparison functions.
#
# Here we are only interested in the greater than and less than
# comparison functions.
#
my(undef, undef, $apx_gt, undef, $apx_lt) = generate_relational(1.5e-5);

or

use Math::Utils qw(:utility);    # Other useful functions

$dir = sign($z - $w);

@ternaries = sign(@coefficients);

EXPORT

All functions can be exported by name, or by using a tag that they're grouped under.

utility tag

sign()

$s = sign($x);
@slist = sign(@values);

Returns -1 if the argument is negative, 0 if the argument is zero, and 1 if the argument is positive.

In list form applies the same operation to each member of the list.

fortran tag

These are functions that originated in FORTRAN, and were implented in Perl in the module Math::Fortran, by J. A. R. Williams.

They are here with a name change -- copysign() was known as sign() in Math::Fortran.

copysign()

 $ms = copysign($m, $n);
 $s = copysign($x);

Take the sign of the second argument and apply it to the first.

If there is only one argument, return -1 if the argument is negative, otherwise return 1.

log10()

$xlog10 = log10($x);
@xlog10 = log10(@x);

Return the log base ten of the argument. A list form of the function is also provided.

compare tag

Create a comparison function for floating point (non-integer) numbers.

Since exact comparisons of floating point numbers tend to be iffy, the functions returns a comparison function using a tolerance chose by the programmer. The programmer may then use that function from then on confident that comparisons will be consistent.

If the programmer does not pass in a tolerance, the comparison function returned will have a default tolerance of 1.49-e8, which is roughly the square root of the machine epsilon on Intel's Pentium chips.

generate_fltcmp()

Returns a comparison function that will compare values using a tolerance that you supply. The generated function will return -1 if the first argument compares as less than the second, 0 if the two arguments compare as equal, and 1 if the first argument compares as greater than the second.

my $fltcmp = generate_fltcmp(1.5e-7);

my(@xpos) = map {&$fltcmp($_, 0) == 1} @xvals;

If you do not provide a tolerance, a default tolerance of 1.49e-8 (approximately the square root of an Intel Pentium's machine epsilon) will be used.

generate_relational()

Returns a list of comparison functions that will compare values using a tolerance that you supply. The generated functions will be the equivalent of the equal, not equal, greater than, greater that or equal, less than, and less than or equal operators.

my($eq, $ne, $gt, $ge, $lt, $le) = generate_relational(1.5e-7);

my(@approx_5) = map {&$eq($_, 5)} @xvals;

Of course, if you were only interested in not equal, you could use:

my(undef, $ne) = generate_relational(1.5e-7);

my(@not_around5) = map {&$ne($_, 5)} @xvals;

Internally, the functions all created using generate_fltcmp().

AUTHOR

John M. Gamble, <jgamble at cpan.org>

BUGS

Please report any bugs or feature requests to bug-math-util at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Utils. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Math::Utils

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2013 by John M. Gamble

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.