Re: computing t = pow(-11.5, .333)
- From: Jordan Abel <random832@xxxxxxxxx>
- Date: 16 May 2006 05:05:59 GMT
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
- References:
- computing t = pow(-11.5, .333)
- From: John Smith
- Re: computing t = pow(-11.5, .333)
- From: Robert Gamble
- Re: computing t = pow(-11.5, .333)
- From: Robert Gamble
- computing t = pow(-11.5, .333)
- Prev by Date: Re: help with types needed
- Next by Date: Re: Introduce urself
- Previous by thread: Re: computing t = pow(-11.5, .333)
- Next by thread: Re: computing t = pow(-11.5, .333)
- Index(es):
Relevant Pages
|