Numeric Functions

awk provides the elementary numeric functions listed in Table 9-5Table 9-5. Most of them are common to many programming languages, and their accuracy depends on the quality of the underlying native mathematical-function library.

Table 9-6. Elementary numeric functions

Function

Description

atan2( y, x )

Return the arctangent of y/x as a value in - to +.

cos( x )

Return the cosine of x (measured in radians) as a value in -1 to +1.

exp( x )

Return the exponential of x, e x.

int( x )

Return the integer part of x, truncating toward zero.

log( x )

Return the natural logarithm of x.

rand( )

Return a uniformly distributed pseudorandom number, r, such that 0 r < 1.

sin( x )

Return the sine of x (measured in radians) as a value in -1 to +1.

sqrt( x )

Return the square root of x.

srand( x )

Set the pseudorandom-number generator seed to x, and return the current seed. If x is omitted, use the current time in seconds, relative to the system epoch. If srand( ) is not called, awk starts with the same default seed on each run; mawk does not.

The pseudorandom-number generator functions rand() and srand( ) are the area of largest variation in library functions in different awk implementations because some of them use native system-library functions instead of their own code, and the pseudorandom-number generating algorithms and precision vary. Most algorithms for generation of such numbers step through a sequence from a finite set without repetition, and the sequence ultimately ...

Get Classic Shell Scripting 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.