Perl Programming Page 8 - Perl 101 (part 6) - The Perl Toolbox |
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 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 = 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.
blog comments powered by Disqus |