I'm working on an application that requires arbitrary precision calculations. There is no power function for computing a number to the power of another. However, there are functions for computing exp(x), mul(x, y) and ln(x), so I'm using the identity x ^ y = exp(y * ln(x)) to compute exponentiation. The functions allow me specifying the number of decimals (scale) as the precision. For example: ln(x, 10) will return a result precise up to the 10th decimal place.

The question is: how to compute the number of decimals places needed for computing the exponentiation precise up to a given scale? In other words, given the function pow(x, y, s), where x is the base, y the exponent and s the scale (number of decimal places), what should be the value of s1 in such way that the result will be exact up to the sth decimal place?

pow(x, y, s) => exp(mul(y, ln(x, s1), s1), s1)

  • $\begingroup$ Is your error scale relative or absolute? If it is relative (and small of course) then $\exp(z)$ has a relative error of $\epsilon$ provided $z$ has an absolute error a little bit less than $\epsilon$. If it is absolute then the problem is more difficult. Usually people stick to relative error in these kinds of things. (Incidentally, I'm sure such a library as this one already exists and is probably even open source.) $\endgroup$ – Ian Aug 18 '17 at 2:55
  • $\begingroup$ I'm using bcmath that requires specifying the exacly numer of decimal places. Example: exp(1, x) returns the the euler number with x decimals places, precisely. So I've to rougly estimate this number to compute the exponentiation precisely up to a given scale. I tried estimatiing using log10, but for fractional powers it does not work. $\endgroup$ – Marcos Passos Aug 18 '17 at 3:09
  • $\begingroup$ So it's absolute error scale. That's problematic, because it means you have to estimate the answer in order to estimate how accurate the exponent must be. It is probably easier to proceed iteratively: get the answer with a prespecified relative error (say $10^{-16}$ a la double precision floating point), then determine whether that was good enough for your absolute error scale, if not then continue improving it. $\endgroup$ – Ian Aug 18 '17 at 3:33
  • $\begingroup$ (Cont.) Determining how to get a relative error of at most $10^{-16}$ is not hard using the Lagrange remainder and the trivial identity $\exp(-x)=1/\exp(x)$: assume without loss of generality that $x \geq 0$ and then just continue adding Taylor terms until $x^{n+1}/(n+1)!<10^{-16}$. $\endgroup$ – Ian Aug 18 '17 at 3:36
  • $\begingroup$ For $x$ close to zero, some libraries provide special routines for computing $e^x-1$ and $\ln(1+x)$. $\endgroup$ – marty cohen Aug 18 '17 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.