Perl 101 (part 6) - The Perl Toolbox - Math Class (
Page 8 of 8 )
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:
#!/usr/bin/perl
# 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");
}
else
{
$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");
}
else
{
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:
#!/usr/bin/perl
print rand(9);
And this could return
7.06539493566379
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:
#!/usr/bin/perl
# 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.