Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>pow(a,b)</code> is often thought of, defined as, and implemented as <code>exp(log(a)*b)</code> where <code>log(a)</code> is natural logarithm of a. log(a) is not defined for a&lt;=0 in real numbers. So you need to either write a function with special case for negative a and integer <code>b</code> and/or <code>b=1/(some_integer)</code>. It's easy to special-case for integer <code>b</code>, but for <code>b=1/(some_integer)</code> it's prone to round-off problems, like Sven Marnach pointed out.</p> <p>Maybe for your domain <code>pow(-a,b)</code> should always be <code>-pow(a,b)</code>? But then you'd just implement such function, so I assume the question warrants more explanation .</p> <p>Like duskwuff suggested, a much more robust and "mathematical" solution is to use complex functions log and exp, but it's much more "complex" (excuse my pun) than it seems on the surface (even though there's <code>cpow</code> function). And it'll be much slower if you have to compute a lot of pow()s.</p> <p>Now there's an important catch with complex numbers that may or may not be relevant to your problem domain: when done right, the result of <code>pow(a,b)</code> is not one, but often <strong>a few complex numbers</strong>, but in the cases you care about, one of them will be complex number with nearly-zero imaginary part (it'll be non-zero due to roundoff errors) which you can simply ignore and/or not compute in your code.</p> <p>To demonstrate it, consider what <code>pow(-1,.5)</code> is. It's a number <code>X</code> such that <code>X^2==-1</code>. Guess what? There are 2 such numbers: <code>i</code> and <code>-i</code>. Generally, <code>pow(-1, 1/N)</code> has exactly <code>N</code> solutions, although you're interested in only one of them.</p> <p>If the imaginary part of all results of <code>pow(a,b)</code> is significant, it means you are passing wrong values. For single-precision floating point values in the range you describe, 1e-6*max(abs(a),abs(b)) would be a good starting point for defining the "significant enough" threshold. The extreme "wrong values" would be <code>pow(-1,0.5)</code> which would return <code>0 + 1i</code> (<code>0</code> in real part, <code>1</code> in imaginary part). Here the imaginary part is huge relative to the input and real part, so you know you screwed up your input values. </p> <p>In any reasonable single-return-result implementation of <code>cpow()</code> , <code>cpow(-1,0.3333)</code> will probably return something like <code>-1+0.000001i</code> and ignore two other values with significant imaginary parts. So you can just take that real value and that's your answer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload