0

Does anyone know an algorithm for a Pow2() function for fractions?

For a function of the following form, where BigRational stands for any rational number type, with arbitrarily large integers for numerator and denominator:

BigRational Pow2(BigRational exponent, int maxdigits)

For the inverse function, BigRational Log2(BigRational exponent, int maxdigits), I have already found a very nice algorithm that uses several identities, converges quickly, and is many times faster than corresponding Log (Ln) or Log10 functions.

Of course, Pow2(x) works with Exp(x * Log(2)) but the point is to avoid Exp as this function is relatively slow for arbitrary precesission arithmetic.

Currently I work on an library for arbitrary precision arithmetic based on some new foundations:
https://github.com/c-ohle/RationalNumerics

An efficient algorithm for such a Pow2 function that is more powerful than Exp (based on Taylor series) could improve the performance of many other functions and algorithms for arbitrary arithmetic.

Christian Ohle
  • 143
  • 1
  • 4
  • My first instinct would be to resort to power series (Taylor series) or continued fraction for such things. Where are you at now? Can we set the bar for "efficient" by disclosing your current best approach? – Wyck Jun 09 '22 at 18:54
  • Sure, any approach would be helpful. After lot research I found nothing with Taylor or continued fractions. Only always again the common exp improvements and floating point tricks. – Christian Ohle Jun 09 '22 at 19:01
  • You realize that `pow(2, x)` is not closed over the rational numbers, correct? So you are going to produce approximations. How accurate do the approximations need to be? – Ben Voigt Jun 09 '22 at 21:35
  • Specifically, for non-negative integer inputs, Pow2 gives integer output. For signed integer inputs, Pow2 gives rational numbers. For rational inputs which are not integers, Pow2 gives irrational outputs. – Ben Voigt Jun 09 '22 at 21:37
  • Yes, that's right, the results are not (always) rational but should be an approximation up to an desired precesission. This precisission is a further variable parameter for the function. Selfexplaning that for up to 15 decimal digits the best choice is to convert the fraction by division to a floating point type like Double... but for a arbitrary rational number class much more digits are desirable. – Christian Ohle Jun 09 '22 at 23:08

0 Answers0