The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Math::Simple - Very simple, commonly used math routines


Version "2.1.2"


 use Math::Simple qw(log2);
 my $low            =  min($a,$b, $c, $d ...);    # least positive num
 my $hi             =  max($a,$b);                # most positive num
 my $digits_ƒ       =  sub ($) {int log10($_[0]); # log10 default export  
 my $log_in_base    =  logb($base,$num);          # log arbitrary base
 my $log16_ƒ        =  logb(16);                  # create log16 func 
 my $bits_needed_ƒ  =  sub ($) {int log2($_[0])};
 use constant nbits => log2(~1);                  # compile constant
 my $gcd            =  gcd(42,12)


Very simple math routines that I end up re-using in many progs and libified for easy access.

Most of the functions are exported by default, with easy options to unexport the ones you don't. As of this version, default exports are min, max, log10, logb, and gcd, with log2 being an optional export.

Note on the logb function. It returns a function or a result based on the number of inputs. If one input is given, it is a logarithm function factory -- producing a specialized, or curried log function for the base given. If the logb function is given two parameters, it will return the logarithm of the second number in the base of the first.

Math::Simple uses Xporter, so including log2 or any future additions won't break the default EXPORT list see Xporter.

Performance Note

In order to calculate the log in another base, one must first take the log of the base in a known base (Perl's natural logarithm function is used for this). This means that non-natural logarithm bases normally require 2 logarithms/call, however, the bases are cached the first time used so future calls will only need the logarithm function called on the new data.