AWK - Arithmetic Functions



AWK has the following built-in arithmetic functions −

atan2(y, x)

It returns the arctangent of (y/x) in radians. The following example demonstrates this −

Example

[jerry]$ awk 'BEGIN {
   PI = 3.14159265
   x = -10
   y = 10
   result = atan2 (y,x) * 180 / PI;
   
   printf "The arc tangent for (x=%f, y=%f) is %f degrees\n", x, y, result
}'

On executing this code, you get the following result −

Output

The arc tangent for (x=-10.000000, y=10.000000) is 135.000000 degrees

cos(expr)

This function returns the cosine of expr, which is expressed in radians. The following example demonstrates this −

Example

[jerry]$ awk 'BEGIN {
   PI = 3.14159265
   param = 60
   result = cos(param * PI / 180.0);

   printf "The cosine of %f degrees is %f.\n", param, result
}'

On executing this code, you get the following result −

Output

The cosine of 60.000000 degrees is 0.500000.

exp(expr)

This function is used to find the exponential value of a variable.

Example

[jerry]$ awk 'BEGIN {
   param = 5
   result = exp(param);
   
   printf "The exponential value of %f is %f.\n", param, result
}'

On executing this code, you get the following result −

Output

The exponential value of 5.000000 is 148.413159.

int(expr)

This function truncates the expr to an integer value. The following example demonstrates this −

[jerry]$ awk 'BEGIN {
   param = 5.12345
   result = int(param)
   
   print "Truncated value =", result
}'

On executing this code, you get the following result −

Truncated value = 5

log(expr)

This function calculates the natural logarithm of a variable.

Example

[jerry]$ awk 'BEGIN {
   param = 5.5
   result = log (param)
   
   printf "log(%f) = %f\n", param, result
}'

On executing this code, you get the following result −

Output

log(5.500000) = 1.704748

rand

This function returns a random number N, between 0 and 1, such that 0 <= N < 1. For instance, the following example generates three random numbers

Example

[jerry]$ awk 'BEGIN {
   print "Random num1 =" , rand()
   print "Random num2 =" , rand()
   print "Random num3 =" , rand()
}'

On executing this code, you get the following result −

Output

Random num1 = 0.237788
Random num2 = 0.291066
Random num3 = 0.845814

sin(expr)

This function returns the sine of expr, which is expressed in radians. The following example demonstrates this −

Example

[jerry]$ awk 'BEGIN {
   PI = 3.14159265
   param = 30.0
   result = sin(param * PI /180)

   printf "The sine of %f degrees is %f.\n", param, result
}'

On executing this code, you get the following result −

Output

The sine of 30.000000 degrees is 0.500000.

sqrt(expr)

This function returns the square root of expr.

Example

[jerry]$ awk 'BEGIN {
   param = 1024.0
   result = sqrt(param)
   
   printf "sqrt(%f) = %f\n", param, result
}'

On executing this code, you get the following result −

Output

sqrt(1024.000000) = 32.000000

srand([expr])

This function generates a random number using seed value. It uses expr as the new seed for the random number generator. In the absence of expr, it uses the time of day as the seed value.

Example

[jerry]$ awk 'BEGIN {
   param = 10
   
   printf "srand() = %d\n", srand()
   printf "srand(%d) = %d\n", param, srand(param)
}'

On executing this code, you get the following result−

Output

srand() = 1
srand(10) = 1417959587
awk_built_in_functions.htm
Advertisements