Perl Information Center Tutorials - Math Functions
These tutorials were written to help you get a quick, but thorough, understanding of Perl -
the scope of the language as well as it's specific capabilities.
| Beginners
| Built-In Functions
| Advanced
| CGI Applications
|
|
|
|
|
|
Arithmetic Functions Summary
As languages go, Perl provides relatively few arithmetic functions.
Example usage of each function is provided below, but as you can
see the functions are very simple to use. The arithmetic functions
can be categorized roughly into four areas:
|
| atan2, cos, sin
|
|
| exp, log, sqrt
|
|
| abs, hex, int, oct
|
|
| rand, srand
|
Arithmetic Function Reference
Here's a quick reference of the available arithmetic functions, in alphabetical
order. The functions are very simple to use.
Unless otherwise noted, these functions operate on $_ by default.
- abs - absolute value
$result = abs $var
- atan2 - arctangent of Y/X in range -pi to +pi
$result = atan2 $y,$x
- cos - cosine
$result = cos (expression) # radians
- exp - exponentiation
$result = exp (expression) # e to the power of expression
- hex - convert to hex
$result = hex (expression) # integers only
- int - integer portion of a number
$result = int (expression) # truncates toward zero
- log - natural logarithm
$result = log (expression) # base e logarithm
- oct - convert to octal number
$result = oct (expression)
- rand - return random number
$result = rand (expression) # returns value >=0 and < expression
# expression should be positive
# default is to operate on "1"
# auto-calls srand
# int(rand(10)) - integer 0-9
- sin - sine
$result = sin (expression) # radians
- sqrt - square root
$result = sqrt (expression) # positive expression required
- srand - seeds the random number generator
$result = srand (expression) # call only one time per script
|