1
$\begingroup$

this is realistically for a programming project, but is more math centric then CS centric. I am attempting to write a function that approximates a power function, but in order to complete I need to approximate log2(x). using the understanding that

$$x^y = x^{a+b+c+d+\dots} = x^a*x^b*x^c*x^d\dots || y = a+b+c+d+\dots$$

and that

$\displaystyle x^y = 2^{y\log_2(x)}$ computers think in base $2$ easier then base $10$, or $e$

since this would only be used for values $y \in [1,2)$ the solution only needs to be accurate on those bounds. considering that $log_c(a*c^x) = log_c(a) + x$ by factorization (not necessarily prime) I can reduce the input to a value on those bounds.

I need the RHS of $\displaystyle log_2(x) = || x \in [1,2)$

defined in addition, subtraction, multiplication, and division. whole number exponents are also acceptable.

EDIT: did some analysis, and updated question.

  • 0
    Is it $x$ or $y$ in $[0,1)$? You might want to avoid $\log 0$.2012-03-18
  • 0
    @henry log(0) will be avoided by breaking out of the function if x = 0 resulting in 0. considering 0^x = 0, except 0^0 which is by calculus definition 1. will think on the bounds. please keep question open for edits2012-03-18
  • 1
    @gardian06 I _strongly_ recommend not defining log(0) as 0. Return NaN or -Inf if you have access to it, throw an exception if you need to, but given that log(1)=0 and log(x) is negative for x < 1, returning log(0) as 0 is only begging for a host of subtle bugs.2012-03-18
  • 0
    Which do you want, $\log_2(x)$ or $x^y$? Are you using floating-point or fixed-point? How much accuracy do you need?2012-03-18
  • 0
    @stevenstadnicki I didn't say that I would define log(0) = 0. I said that it would be avoided by if the base of the exponent is 0 to return 0, or 1 depending on the power. as 0^x || x != 0 = 0, and 0^0 is 12012-03-18
  • 0
    @robertisrael I wanted $log2(x)$ which will be called from $x^y$, and floating point numbers. as for the most part CS says that it is whole, or floating, and defining an exponential function purely in terms of whole numbers would yield inappropriate returns2012-03-18
  • 0
    @gardian06 Ahhh - it wasn't clear to me from your comment that you were only using log in service of computing $x^y$ and that 'breaking out of the function' meant breaking out of the $x^y$ function. That said, without knowing more about what you're using this exponentiation for it's hard to give good advice - numerical routines are by their nature often very problem-dependent in the details...2012-03-18
  • 0
    @stevenstadnicki the direct upfront design is for physics motion equations, but the big thing of CS programming is re-usability, and I am attempting to put in the lead cases, but the overall approximations is what I need. see question update2012-03-18
  • 0
    So much work has been done on computing the elementary functions that it seems to me that you should investigate what has been done. In particular, for log you should look at ln(1+x) for x in [0, 1) (and for exp. look at exp(x)-1 rather than exp(x)).2012-03-18
  • 0
    @gardian06 Not to keep prying too much, but what sort of motion equations? I know of very few offhand that require raising arbitrary numbers to arbitrary non-integer powers, and both $e^x$ and $x^n$ for integer $n$ can be calculated both faster and more accurately than general $x^y$ routines will give.2012-03-18
  • 0
    @stevenstadnicki this requires a small number of assumptions. taking that $x=a*t^2+v*t+x_0$ the effect of $at^2$ is relatively small to have object be visible difference, but $a$ needs to be considered per frame (which can vary largely based on many factors), but if we take generic drag without breaking out into surface pressure we can apply $v_f = v_i + a*d^t$, and $x_f = x_i + v_f*d^t$ and considering that $t$ is inversely proportional to the frame rate then we get what appears to be general resistance to motion, and smooth degradation of motion2012-03-18
  • 0
    @gardian06 From your comment I'm going to presume that you're trying to essentially solve some differential equations-of-motion with frictional drag under variable frame rates, in which case I think you're solving exactly the wrong problem here by taking the exponential approach you're trying here - there are much better ways of doing this. I'd heartily encourage posting your _original_ problem ('how do I simulate this motion?') to something like the gamedev stackexchange site; you'll get much better answers to your real question than you're likely to get from this approach.2012-03-18

4 Answers 4