Perl | Useful Math functions
In Perl, sometimes there is a need to solve various expressions that contain some mathematical operations. These mathematical operations can be performed with the use of various inbuilt-functions.
#!/usr/bin/perl # Initialising some values for the # parameter of the exp function $A = 0; $B = 1; # Calling the exp function $E = exp $A; $F = exp $B; # Getting the value of "e" raised to the # power of the given parameter. print "$E\n"; print "$F\n"; # Calculating square root using sqrt() $square_root = sqrt(64); # Printing the result print "Squareroot of 64 is: $square_root"; |
Some useful functions for mathematical operations in Perl are listed below:
| Function | Description |
|---|---|
| exp() | Calculates “e” raised to the power of the real number taken as the parameter |
| hex() | Converts the given hexadecimal number ( of base 16 ) into its equivalent decimal number ( of base 10 ). |
| srand() | Helps rand() function to generate a constant value each time the program is run |
| sqrt() | Used to calculate the square root of a number |
| oct() | Converts the octal value passed to its respective decimal value |
| rand() | Returns a random fractional number between 0 and the positive number value passed to it, or 1 if no value is specified |
| log() | Returns the natural logarithm of value passed to it. Returns $_ if called without passing a value |
| int() | Returns the integer part of given value. It returns $_ if no value provided |
| sin() | Used to calculate sine of a VALUE or $_ if VALUE is omitted |
| cos() | Used to calculate cosine of a VALUE or $_ if VALUE is omitted |
| atan2() | Used to calculate arctangent of Y/X in the range -PI to PI. |
| abs() | Returns the absolute value of its argument |


Please Login to comment...