Algorithms to calculate elementary functions with arbitrarily high precision can also be fount in the sources of the python package mpmath. The code is well commented. Andrey