Purpose

Serve as a general-purpose calculator, evaluating arithmetic
statements of arbitrary complexity. In addition to the normal
functions provided by AWK, this script offers the trigonometric
functions available with GAWK.

Usage

calc [expr]

Arguments

expr = an arithmetic expression

Output

resultant value of the operation

Restrictions

Expressions involving parentheses "()" and/or the
multiplication operator "*" must be enclosed in double quotes
to prevent conflicts with shell expansion.

Note

In addition to all of the usual GAWK functions, the following

functions are available for user convenience

abs(x) = absolute value of x
nint(x) = nearest integer
log10(x) = common logarithm of x

sind(x) = sine of x (x in degrees)
cosd(x) = cosine of x (x in degrees)

asin(x) = inverse sine of x (result in radians)
acos(x) = inverse cosine of x (result in radians)
atan(x) = inverse tangent of x (result in radians)
asind(x) = inverse sine of x (result in degrees)
acosd(x) = inverse cosine of x (result in degrees)
atan2d(x,y) = inverse tangent of x,y (result in degrees)

Exit values

0 = normal completion
1 = no arguments provided
2 = arithmetic error

Example

calc 1+2 # returns "3"
calc 1 + 2 + 3 # returns "6"
calc "(1+2+3)*4/6" # returns "4"
calc "(1+2+3)*4/6." # returns "3.93443"
calc "int(1.4)" # returns "1"
calc "2*pi" # returns "6.28319"

set a = 1000
calc $a+1 # returns "1001"

calc "sind(45)" # take sin of 45deg; returns "0.707107"