eHow launches Android app: Get the best of eHow on the go.

About

Perl Script Calculation

Contributor
By Phaedra Krizo
eHow Contributing Writer
(0 Ratings)

The following is a strategy guide to performing mathematical calculations using the Perl programming language. Included is a list of numerical operators, a guide to performing basic mathematical functions and examples of how to include Perl math operators and functions in your Perl program. Because Perl only defines the sine and cosine trigonometric functions, this article will also explain how to use the Math::Trig module in your program to perform additional trig and math calculations.

    Perl Numeric Operators

  1. The following is a list of the Perl numeric operators and an explanation of what they do.

    Addition: "+" is plus.
    For example, the line of code "4 + 5" returns 9.

    Subtraction: "-" is minus.
    For example, the line of code "11.3 - 4" returns 7.3.

    Multiplication: "*" is times.
    For example, the line of code "3 * 7" returns 21.

    Division (floating point): "/" is divided by.
    For example, the line of code "10 / 3" returns 3.33333....

    Remainder: "%" is mod (returns the remainder after a number is divided by the divisor).
    For example, the line of code "7 % 3" returns 1.

    Exponents: "**" is to the power of.
    For example, the line of code "2**3" returns 8.
  2. Mathematical Functions

  3. To find the square root of x: returnValue = sqrt (x);
    To find the absolute value of x: returnValue = abs (x);
    To find the natural (base e) logarithm of the x: returnValue = log (x);
  4. Trigonometric Functions

  5. To find the sine of x (in radians): returnValue = sin (x);
    To find the cosine of x (in radians): returnValue = cos (x);
  6. Math::Trig Functions---Getting Started

  7. Additional math functions not provided by the basic Perl language can be imported from the Math::Trig module. To use this module, insert the following code into the beginning of your Perl program (just after #!/usr/bin/perl):
    use Math::Trig;
  8. Some Math::Trig Functions

  9. tan(x)--returns the tangent of x
    acos(x)--returns the inverse cosine of x
    asin(x)--returns the inverse sine of x
    pi--returns the value of pi
    deg2rad(x--converts the value of x (degrees) into radians

    For example, the following code:
    $returnValue = pi;
    Results in:
    The value of returnValue is equal to pi.
  10. More Math::Trig Functions--Conversions

  11. The following functions are used to perform conversions between degrees, radians and gradians. (Note: A full circle = 2 pi radians = 360 degrees = 400 gradians.)

    deg2rad(x)--converts the value of x (degrees) into radians
    grad2rad(x)--converts the value of x (gradians) to radians
    rad2deg(x)--converts the value of x (radians) to degrees
    grad2deg(x)--converts the value of x (gradians) into degrees
    deg2grad(x)--converts the value of x (degrees) into gradians
    rad2grad(x)--converts the value of x (radians) to gradians
Subscribe

Post a Comment

Post a Comment Post this comment to my Facebook Profile

Related Ads

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

eHow Computers
eHow_eHow Technology and Electronics