------------------------------------------------------------------------------- bc - Basic Calculator This is a arbitrary-precision infix calculator, that used to compile its functions into the "dc" stack machine. The syntax is simular too but not the same as 'C'. NOTES: * Fixed point precision (scale) is 0, unless -l is used (scale=20) * The ';' is optional if followed by a new line * A expression that is NOT assigned is printed (or prefix "print") * The modulo '%' will fail when scale is not 0 * Comments are C-like "/* ... */" * POSIX can only use single letters for function and variable names!!! * GNU-bc can use '#' for comments, and longer variable names * the power-of '^' can only use interger exponents Examples... echo '6.5 / 2.7' | bc 2 echo 'scale=3; 6.5 / 2.7' | bc 2.407 echo '6.5 / 2.7' | bc -l 2.40740740740740740740 Variables can be used The keywork 'auto' can define them local to a function. bc <<< ' scale=3; var1 = 6.5 / 2.7; var2 = 14 * var1; var2 ' 33.698 ------------------------------------------------------------------------------- Math Functions (use a -l option - also sets scale to 20) s (x) # The sine of x, x is in radians. c (x) # The cosine of x, x is in radians. a (x) # The arctangent of x, arctangent returns radians. l (x) # The natural logarithm of x. e (x) # The exponential function of raising e to the value x. j (n,x) # The bessel function of integer order n of x. sqrt(x) # Square root of the number x. pi=a(1)*4 # set the value of PI echo 'pi=a(1)*4; d2r=pi/180; c(30*d2r); s(30*d2r)' | bc -l .86602540378443864690 .49999999999999999976 /* power-of with non-integers */ /* Use the fact that x^y == e^(y*log(x)) */ define p(x,y) { return ( e( y * l(x) ) ) } ------------------------------------------------------------------------------- Conditional if(condition) {statements} else {statements} Iterative Statements for (assignment; condition; increment) {statements} while (condition) {statements} User defined define name(comma separated parameters list) { statements return statement } -------------------------------------------------------------------------------