Perl
  Home arrow Perl arrow Page 8 - Perl 101 (part 6) - The Perl Toolbox
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Perl 101 (part 6) - The Perl Toolbox
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2000-08-30


    Table of Contents:
  • Perl 101 (part 6) - The Perl Toolbox
  • Expressing Yourself
  • Engry Young Men
  • Aardvark, Anyone?
  • Needles In Haystacks
  • Slice And Dice
  • Going Backwards
  • Math Class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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.

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

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek