Re: computing t = pow(-11.5, .333)



On 2006-05-16, Robert Gamble <rgamble99@xxxxxxxxx> wrote:
Robert Gamble wrote:
John Smith wrote:
In a C program I need to do exponentiation where the base is
negative and the exponent is a fraction. In standard C this would
be something like t = pow(-11.5, .333), but with this combination
of arguments there is a domain error and the result is a
percolating NaN.

I worked around the problem by putting the standard function in a
wrapper:

double xpow(double b, double x)
{
double p;

if(b < 0)
{
p = pow(-b, x);
p *= -1.0;
}
else p = pow(b, x);

return p;
}

Is there any particular reason for this limitation in the
standard library function? Has anyone else had a problem with
this and found a better solution?

I don't know why the pow function is defined this way and there is
little insight in the rationale.

I obviously didn't give enough thought to that. Any negative number
raised to a fractional power that is not the reciprocal of an odd
number (1/3, 1/5, 1/7, etc) will have a complex result.

Or with such reciprocals - I believe that, traditionally, raising
a negative number to the fractional power yields the root with the
lowest [counterclockwise] angle on the complex plane from the positive
real axis - which is going to be less than pi (the angle for negative
numbers), while the nth-root operator yields the negative real root.

C99 provides
support for complex numbers and you can perform such calculations with
the cpow family of functions.

You can obtain the expected result of
pow(-11.5, .333) by using the cube root function from C99 if your
implementation provide it: cbrt(-11.5);.

Robert Gamble

.



Relevant Pages

  • Re: E96 Series Computation
    ... >>>3) multiply remaining fraction by 64 ... >>>absolute closest standard value to arbitrary input R are hopelessly lost ... >>>as noise compared to tolerance. ... >> I guess I don't understand what your algorithm is supposed to do. ...
    (sci.electronics.design)
  • Re: Sichere suid-Dateizugriffe programmieren
    ... Diesen "Standard" möchte ich auch gern sehen. ... In dem Fall: Ja, keine gute Idee. ... Aber blind den User der ersten Pfadkomponente zu nehmen, die nicht "root" ... ACK, und daher sollte man dann den *richtigen* Benutzer nehmen, und ...
    (de.comp.os.unix.programming)
  • Re: An uncountable countable set
    ... All paths start at the root. ... it is only unsplit edges and unsplit paths that are to be matched ... Any proper fraction of one won't reach. ... In the set of naturals is the first. ...
    (sci.math)
  • Re: No longer prompted for password!?!
    ... > where bloggs is some user. ... > Check that you are not root ... This is a standard MandrivaLinux SE 2005 installation and I'm behind a ...
    (comp.os.linux.misc)
  • Re: Forgot root password. How to reset it?
    ... depending on the user and/or way of connecting. ... I did that once so that you get different colours for standard users, ... root, telnet and ssh connections. ...
    (alt.os.linux.suse)