The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

POSIX::1003::Math - POSIX handling time

INHERITANCE

 POSIX::1003::Math
   is a POSIX::1003

SYNOPSIS

  use POSIX::1003::Math qw/ceil floor sqrt/;
  print ceil 3.14;
  print sqrt floor 4.9;

DESCRIPTION

Be aware that math in Perl has unclear precission! Be aware that the math library often provides many variations of these functions... it is hard to determine which one is used. Probably, Math::Trig will serve you better. Or PDL for real number crunchers.

Be warned that these functions do not have an obligatory scalar parameter, but only an optional parameter (defaults to $_). This means that they have the lowest (is list) priority.

FUNCTIONS

Standard POSIX via this module (via POSIX.xs)

Like the built-in sin, cos, and sqrt, the EXPR defaults to $_ and there is a scalar context (missing from POSIX.pm).

acos(EXPR)
asin(EXPR)
atan(EXPR)
ceil(EXPR)
cosh(EXPR)
div(NUMER, DENOM)

Devide NUMER by DENOminator. The result is a list of two: quotient and remainder. Implemented in Perl for completeness, currently not with the speed of XS.

example:

  my ($quotient, $remainder) = div($number, $denom);
floor(EXPR)
fmod(EXPR, EXPR)
frexp(EXPR)
ldexp(EXPR)
log10(EXPR)
modf(EXPR)
pow(EXPR1, EXPR2)

Returns EXPR1 ** EXPR2

rint(NUMBER)

Round to the closest integer. Implemented in Perl for completeness.

sinh(EXPR)
tan(EXPR)
tanh(EXPR)

Standard POSIX, using CORE

A small set of mathematical functions are available in Perl CORE, without the need to load this module. But if you do import them, it simply gets ignored.

abs([EXPR])
atan2(EXPR, EXPR)
cos([EXPR])
exp([EXPR])
log([EXPR])
rand([EXPR])
sin([EXPR])
sqrt([EXPR])
srand([EXPR])

Numeric conversions

All strto*, atof, atoi and friends functions are usually not needed in Perl programs: the integer and float types are at their largest size, so when a string is used in numeric context it will get converted automatically. Still, POSIX.xs does provide a few of those functions, which are sometimes more accurate in number parsing for large numbers.

All three provided functions treat errors the same way. Truly POSIX-compliant systems set $ERRNO ($!) to indicate a translation error, so clear $! before calling strto*. Non-compliant systems may not check for overflow, and therefore will never set $!.

To parse a string $str as a floating point number use

  $! = 0;
  ($num, $n_unparsed) = strtod($str);

  if($str eq '' || $n_unparsed != 0 || $!) {
      die "Non-numeric input $str" . ($! ? ": $!\n" : "\n");
  }

  # When you do not care about handling errors, you can do
  $num = strtod($str);
  $num = $str + 0;     # same: Perl auto-converts
strtod(STRING)

String to double translation. Returns the parsed number and the number of characters in the unparsed portion of the string. When called in a scalar context strtod returns the parsed number.

strtol(STRING, BASE)

String to integer translation. Returns the parsed number and the number of characters in the unparsed portion of the string. When called in a scalar context strtol returns the parsed number.

The base should be zero or between 2 and 36, inclusive. When the base is zero or omitted strtol will use the string itself to determine the base: a leading "0x" or "0X" means hexadecimal; a leading "0" means octal; any other leading characters mean decimal. Thus, "1234" is parsed as a decimal number, "01234" as an octal number, and "0x1234" as a hexadecimal number.

strtoul(STRING, BASE)

String to unsigned integer translation, which behaves like strtol.

CONSTANTS

Math constants from float.h or limits.h

 DBL_DIG DBL_EPSILON DBL_MANT_DIG DBL_MAX DBL_MAX_10_EXP
 DBL_MAX_EXP DBL_MIN DBL_MIN_10_EXP DBL_MIN_EXP FLT_DIG
 FLT_EPSILON FLT_MANT_DIG FLT_MAX FLT_MAX_10_EXP FLT_MAX_EXP
 FLT_MIN FLT_MIN_10_EXP FLT_MIN_EXP FLT_RADIX FLT_ROUNDS
 LDBL_DIG LDBL_EPSILON LDBL_MANT_DIG LDBL_MAX LDBL_MAX_10_EXP
 LDBL_MAX_EXP LDBL_MIN LDBL_MIN_10_EXP LDBL_MIN_EXP

Math constants from math.h

 HUGE_VAL

Math constants from stdlib.h

 RAND_MAX
 CHAR_BIT CHAR_MAX CHAR_MIN UCHAR_MAX SCHAR_MAX SCHAR_MIN
 SHRT_MAX SHRT_MIN USHRT_MAX
 INT_MAX INT_MIN UINT_MAX
 LONG_MAX LONG_MIN ULONG_MAX

SEE ALSO

This module is part of POSIX-1003 distribution version 0.03, built on December 22, 2011. Website: http://perl.overmeer.net. The code is based on POSIX, which is released with Perl itself.

COPYRIGHTS

Copyrights of the perl code and the related documentation by 2011 by Mark Overmeer. For other contributors see ChangeLog.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html