AWK – Arithmetic Functions

  • Post author:
  • Post category:AWK
  • Post comments:2 Comments
AWK - Arithmetic Functions

This topic is about 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

In this topic we learned about AWK – Arithmetic Functions. To know more, Click Here.

This Post Has 2 Comments

Leave a Reply