Home arrow Perl Programming arrow Page 8 - Perl 101 (part 6) - The Perl Toolbox

Math Class - Perl

This time, Perl 101 visits some of Perl's more useful in-builtfunctions, and teaches you the basics of pattern matching and substitution.Also included is a list of common string and math functions, together withexamples of how to use them.

  1. Perl 101 (part 6) - The Perl Toolbox
  2. Expressing Yourself
  3. Engry Young Men
  4. Aardvark, Anyone?
  5. Needles In Haystacks
  6. Slice And Dice
  7. Going Backwards
  8. Math Class
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
August 30, 2000

print this article


And finally, Perl also comes with a set of math functions that allow you to carry out complex mathematical operations. You probably won't need these, but you should at least know of their existence.

Sine of a angle: sin($radians)

Cosine of a angle: cos($radians)

Square root of a number: sqrt($variable)

Exponent of a number: exp($variable)

Natural logarithm of a number: log($variable)

Absolute value of a number: abs($variable)

Decimal value of a number from hexadecimal: hex($variable)

Decimal value of a number from octal: oct($variable)

Integer portion of a number: int($variable)

And here's an example that demonstrates all these:
# set up the choices
print "Pick from the choices below:\n";
print "Sine of an angle[1]\n";
print "Cosine of an angle[2]\n";
print "Square root of a number[3]\n";
print "Exponent of a number[4]\n";
print "Natural logarithm of a number[5]\n";
print "Absolute value of a number[6]\n";
print "Decimal value of a number from hexadecimal[7]\n";
print "Decimal value of a number from octal[8]\n";
print "Integer value[9]\n";
chomp($choice = );
# and process them
if ($choice == 1 || $choice == 2)
print "Enter the angle in radians: ";
chomp($angle = );
if($choice == 1)
$value = sin($angle);
print("Sine of $angle is $value\n");
$value = cos($angle);
print("Cosine of $angle is $value\n");
elsif($choice == 3)
print "Enter a positive number: ";
chomp($number = );
$value = sqrt($number);
print("The square root of $number is $value\n");
elsif($choice == 4)
print "Enter a number: ";
chomp($number = );
$value = exp($number);
print("e ** $number = $value\n");
elsif($choice == 5)
print "Enter a number: ";
chomp($number = );
$value = log($number);
print("The natural log of $number is  $value\n");
elsif($choice == 6)
print "Enter a number: ";
chomp($number = );
$value = abs($number);
print("The absolute value of $number is $value\n");
elsif($choice == 7)
print "Enter a number: ";
chomp($number = );
$value = hex($number);
print("The decimal value of $number is $value\n");
elsif($choice == 8)
print "Enter a number: ";
chomp($number = );
$value = oct($number);
print("The decimal value of $number is $value\n");
elsif($choice == 9)
print "Enter a number: ";
chomp($number = );
$value = int($number);
print("The integer value of $number is $value\n");
print("Invalid choice\n");

And finally, if you need to use Perl to generate random numbers, you should know about the rand() function. The rand() function takes a number as parameter, and generates a random number between 0 and that number. Here's an example:
print rand(9);

And this could return


If you omit the parameter, you'll get a random number between 0 and 1. And here's a script that asks you for a numerical range, and then returns a random number within that range:

# get the limits
print "Enter the lower limit of the range: ";
$lower = ;
chomp ($lower);
print "Enter the upper limit of the range: ";
$upper = ;
chomp ($upper);
# keep generating until number falls within range
while ($random < $lower)
$random = int(rand($upper));
# then print
print $random;

And that's about all we have time for today. We'll be back with more in a couple of weeks - so keep coming back!

This article copyright Melonfire 2000. All rights reserved.

>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: