Bootstrapping a minimal math library

Sometimes you don’t have all the math functions available that you would like. For example, maybe you have a way to calculate natural logs but you would like to calculate a log base 10.

The Unix utility bc is a prime example of this. It only includes six common math functions:

  • sine
  • cosine
  • arctangent
  • natural log
  • exp
  • square root

Users are expected to know how to calculate anything else they need from there. (Inexplicably, bc also includes a way to calculate Bessel functions.)

This post collects formulas that let you bootstrap the functions listed above into all the trig and hyperbolic functions and their inverses.


Most programming languages provide a way to compute natural logs but not logs in bases other than e. If you have a way to compute logs in base b, you can compute logs in any other base via

\log_a(z) = \frac{\log_b z}{\log_b a}

So, for example, you could compute the log base 10 of a number by computing its natural log and dividing by the natural log of 10.


If you have a way to calculate ex and natural logs, you can compute xy via

x^y = \exp(y \log x)

Since square roots correspond to exponent 1/2, you can use this to compute square roots.

Trig functions

If you have a way to calculate sine and cosine, you can calculate the rest of the six standard trig functions.

\begin{align*} \sec z &= 1/\cos z \\ \csc z &= 1/\sin z \\ \tan z &= \sin z / \cos z \\ \cot z &= \cos z / \sin z \end{align*}

Inverse trig functions

If you have a way to calculate inverse tangent, you can bootstrap it to compute the rest of the inverse trig functions.

\begin{align*} \text{arcsin}(x) &= \arctan(x / \sqrt{1 - x^2}) \\ \text{arccos}(x) &= \arctan(\sqrt{1 - x^2 }/ x) \\ \text{arccot}(x) &= \pi/2 - \arctan(x) \\ \text{arcsec}(x) &= \arctan(\sqrt{x^2 - 1}) \\ \text{arccsc}(x) &= \arctan(1/\sqrt{x^2 - 1}) \end{align*}

Also, you can use arctan to compute π since π = 4 arctan(1).

Hyperbolic functions

If you have a way to compute exponentials, you can calculate hyperbolic functions.

\begin{align*} \sinh(x) &= \frac{\exp(x) - \exp(-x)}{2} \\ \cosh(x) &= \frac{\exp(x) + \exp(-x)}{2} \\ \tanh(x) &= \frac{\exp(x) - \exp(-x)}{\exp(x) + \exp(-x)} \\ \end{align*}

Inverse hyperbolic functions

If you can compute square roots and logs, you can compute inverse hyperbolic functions.

\begin{align*} \text{arcsinh} (z) &= \log \left( z + \sqrt{1 + z^2} \right) \\ \text{arccosh} (z) &= 2 \log\left( \sqrt{(z+1)/2} + \sqrt{(z-1)/2} \right) \\ \text{arctanh} (z) &= \log \left( (1+z) \sqrt{1/(1 - z^2)}\right) \end{align*}

Complex branch cuts

Up to this point this post has implicitly assumed we’re only working with real numbers. When working over complex numbers, inverse functions get more complicated. You have to be explicit about which branch you’re taking when you invert a function that isn’t one-to-one.

Common Lisp worked though all this very thoroughly, defining arc tangent first, then defining everything else in a carefully chosen sequence. See Branch cuts and Common Lisp.

5 thoughts on “Bootstrapping a minimal math library

  1. Part of the fun for me with bc is expanding on the basic setup with functions that behave as one would expect when the scale parameter is changed, so that the functions report accurately to the correct number of digits. In many cases an interesting exercise.

  2. I wonder why they didn’t go further: Since you have the “sin” function and “pi”, the “cos” function is clearly redundant. And since you have “exp” and “ln”, you don’t really need “sqrt”.

    Is the set {sin, atan2, exp, ln} enough to bootstrap a basic math library for real numbers? Can we do better (in terms of less functions or in terms of “more efficient set of functions”)?

  3. Yes, of course, by “they” I meant “the bc designers” (or, more properly, “the dc designers”).

    I was just wondering about the lost of precision and efficiency that results from using such a small math library because the basic idea is very attractive in the microcontroller context but might end up affecting the microcontroller output or its reaction time…

Leave a Reply

Your email address will not be published. Required fields are marked *