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. Zero is considered part of the positive signs.
copysign(-5, 0); # Returns 5.
copysign(-5, 7); # Returns 5.
copysign(-5, -7); # Returns -5.
copysign(5, -7); # Returns -5.
If there is only one argument, return -1 if the argument is negative, otherwise return 1. For example, copysign(1, -4) and copysign(-4) both 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 comparison functions for floating point (non-integer) numbers.
Since exact comparisons of floating point numbers tend to be iffy, the comparison functions use a tolerance chosen by you. You may then use the functions from then on confident that comparisons will be consistent.
If you do not pass in a tolerance, a default tolerance of 1.49-e8 (approximately the square root of an Intel Pentium's machine epsilon) will be used.
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) = grep {&$fltcmp($_, 0) == 1} @xvals;
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) = grep {&$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:
RT: CPAN's request tracker (report bugs here)
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
LICENSE AND COPYRIGHT
Copyright 2015 by John M. Gamble