Arithmetic Functions
Nine of the built-in functions can be classified as arithmetic functions. Most of them take a numeric argument and return a numeric value. Table 9.1 summarizes these arithmetic functions.
Awk Function | Description |
cos(x) | Returns cosine of x (x is in radians). |
exp(x) | Returns e to the power x. |
int(x) | Returns truncated value of x. |
log(x) | Returns natural logarithm (base-e) of x. |
sin(x) | Returns sine of x (x is in radians). |
sqrt(x) | Returns square root of x. |
atan2(y,x) | Returns arctangent of y/x in the range -π to π. |
rand( ) | Returns pseudo-random number r, where 0 <= r < 1. |
srand(x) | Establishes new seed for rand( ). If no seed is specified, uses time of day. Returns the old seed. |
Trigonometric Functions
The trigonometric functions cos( ) and sin( ) work the same way, taking a single argument that is the size of an angle in radians and returning the cosine or sine for that angle. (To convert from degrees to radians, multiply the number by π/180.) The trigonometric function atan2( ) takes two arguments and returns the arctangent of their quotient. The expression
atan2(0, -1)
produces π.
The function exp( ) uses the natural exponential, which is also known as base-e exponentiation. The expression
exp(1)
returns the natural number 2.71828, the base of the natural logarithms, referred to as e. Thus, exp(x) is e to the x-th power.
The log( ) function gives the inverse of the exp( ) function, the natural logarithm of x. The sqrt( ) function takes ...
Get sed & awk, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.