O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The sqrt (expr) function

The sqrt(expr) function returns the positive square root for the given integer evaluated from the expr expression. This function also requires a positive number as an argument; otherwise, it returns an error message NaN for a negative number. It also prints a warning message if a negative number is used as an argument. The following code block shows an example of a sqrt (expr) function:

$ vi sqrtfunc.awkBEGIN    {    print "sqrt(5)    = ", sqrt(5);    print "sqrt(0)    = ", sqrt(0);    print "sqrt(-5)    = ", sqrt(-5);    }$ awk -f sqrtfunc.awk

The output of the execution of the previous code is as follows:

sqrt(5)    =  2.23607sqrt(0)    =  0awk: sqrtfunc.awk:4: warning: sqrt: called with negative argument -5sqrt(-5)    =  -nan

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required