fun with POSIX bc...

By Hal Canary, 2006-06-07 17:53:53 (link)

POSIX defines a command-line calculator language/interpretor known as
bc. bc is convientent but fairly light on the included math
functions. I wanted a power function that would take floating point
numbers as exponents. bc's basic math library provides the following
functions: sqrt(), sin(), cos(), atan(), ln(), and exp()---abriviated
to sqrt(), s(), c(), a(), l(), and e().


a^b = e^( b * ln(a) ),

I can define a new fucntion---

define pow(a,b) { return e(b*l(a)); } 

---for my power function. While I was at it, I went ahead and created
defininitions for the most common trig functions:

define ln(a) { return l(a); }
define exp(a) { return e(a); }
define pow(a,b) { return e(b*l(a)); }
define sqrtt(a) { return e(0.5*l(a)); }
define log(a) { return l(a)/l(10); }
define sin(a) { return s(a); }
define cos(a) { return c(a); }
define tan(a) { return s(a)/c(a); }
define sec(a) { return 1/c(a); }
define csc(a) { return 1/s(a); }
define cot(a) { return c(a)/s(a); }
define asin(x) { return a(x/sqrt(1-(x^2))); }
define acos(x) { return a(sqrt(1-(x^2))/x); }
define atan(x) { return a(x); }
define asec(x) { return a(sqrt((x^2)-1)); }
define acsc(x) { return a(1/sqrt((x^2)-1)); }
define acot(x) { return a(1/x);}
define sinh(x) { return (e(x)-e(-x))/2;}
define cosh(x) { return (e(x)+e(-x))/2;}
define tanh(x) { return (e(2*x) - 1)/(e(2*x) + 1); }
define asinh(x) { return l(x+sqrt((x^2)+1)); }
define acosh(x) { return l(x+sqrt((x^2)-1)); }
define atanh(x) { return 0.5*l((1+x)/(1-x)); }
pi = 4*a(1);

Of course, now that I read more about it I find that I could have just looked up the answers.