Can anyone tell me why pow(8.0, 1.0 / 3.0) (cubic root of 8) returns
nan (in linux) and negative infinity or something (in devcpp in
windows), instead of 2?
The problem seems to be that pow can't handle cubic root of negative
number, I mean, a calculator could do it or am I using the wrong
function here. My current solution is a hack which detects negative
input and handle it differently.
I suspect it is something to do with the fact the power is represented
as a floating point value. 13 5369
Shaobo Hou wrote: Can anyone tell me why pow(8.0, 1.0 / 3.0) (cubic root of 8) returns nan (in linux) and negative infinity or something (in devcpp in windows), instead of 2?
The problem seems to be that pow can't handle cubic root of negative number, I mean, a calculator could do it or am I using the wrong function here. My current solution is a hack which detects negative input and handle it differently.
I suspect it is something to do with the fact the power is represented as a floating point value.
In a way, yes: the second argument to pow() is not
exactly onethird, but a nearby value. You aren't actually
calculating "the cube root of 8," but "8 raised to a
rational exponent close to onethird." That exponent value
is a fraction of the form U/V where U,V are integers and V is
almost certainly a large power of two. Mathematically
speaking,
pow(8, U/V)
== pow(pow(8, 1/V), U)
== pow(sqrt(sqrt(...(8)...)), U)
(since V is a power of two), and this can't be evaluated
in real arithmetic.
Some suggestions:
 The latest "C99" Standard defines a cbrt() function.
Even if you don't have access to a fullblown C99
implementation, you may find that cbrt() is present.
 If cbrt() isn't available, try something like
(x >= 0.0) ? pow(x, 1.0/3.0) : pow(x, 1.0/3.0)
 If your system has copysign(), try writing the above as
copysign(pow(fabs(x), 1.0/3.0), x)
 Er*********@sun.com
On Tue, 22 Feb 2005 15:12:57 +0000, Shaobo Hou said to the parser: Can anyone tell me why pow(8.0, 1.0 / 3.0) (cubic root of 8) returns nan (in linux) and negative infinity or something (in devcpp in windows), instead of 2?
The problem seems to be that pow can't handle cubic root of negative number, I mean, a calculator could do it or am I using the wrong function here. My current solution is a hack which detects negative input and handle it differently.
I suspect it is something to do with the fact the power is represented as a floating point value.
It has more to do with the 8...
The below demonstrates one method for handling this, which may be what you
did already, when you mention your current solution detects negative input
and handles it differently.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
float num = 8;
float result;
if (num >= 0)
result = pow(num, 1.0/3.0);
else
result = pow(num, 1.0/3.0);
printf("%f\n", result);
return EXIT_SUCCESS;
}
In article <cv*********@wapping.cs.man.ac.uk>,
Shaobo Hou <ho***@cs.man.ac.uk> wrote:
:Can anyone tell me why pow(8.0, 1.0 / 3.0) (cubic root of 8) returns
:nan (in linux) and negative infinity or something (in devcpp in
:windows), instead of 2?
:I suspect it is something to do with the fact the power is represented
:as a floating point value.
Plausibly. 1/3 is not exactly representable in binary, so whether
1/3 comes out "odd" or "even" (which would allow you to square the
x first) would be dependant on the precision you are working with.

Those were borogoves and the momerathsoutgrabe completely mimsy.
Eric Sosman wrote:  If cbrt() isn't available, try something like (x >= 0.0) ? pow(x, 1.0/3.0) : pow(x, 1.0/3.0)
This is essentially what I did. thanks
Shaobo Hou wrote: Can anyone tell me why pow(8.0, 1.0 / 3.0) (cubic root of 8) returns nan (in linux) and negative infinity or something (in devcpp in windows), instead of 2?
From C99 section 7.12.7.4, the pow functions:
``A domain error occurs if x is finite and negative and y is finite and
not an integer value.''
pow() is a generic power function and in general a negative number
raised to a nonintegral power will result in a complex number. It would
need to special case x**(1/(2**n)) and x**(1/(2**n+1)) which would be
tricky.
The problem seems to be that pow can't handle cubic root of negative number, I mean, a calculator could do it or am I using the wrong function here. My current solution is a hack which detects negative input and handle it differently.
C99 has cbrt()

Kyle A. York
Sr. Subordinate Grunt
Walter Roberson wrote: In article <cv*********@wapping.cs.man.ac.uk>, Shaobo Hou <ho***@cs.man.ac.uk> wrote: :Can anyone tell me why pow(8.0, 1.0 / 3.0) (cubic root of 8)
returns :nan (in linux) and negative infinity or something (in devcpp in :windows), instead of 2?
:I suspect it is something to do with the fact the power is
represented :as a floating point value.
Plausibly. 1/3 is not exactly representable in binary, so whether 1/3 comes out "odd" or "even" (which would allow you to square the x first) would be dependant on the precision you are working with.
That's nonsense. pow is normally implemented as
pow(a, b) = exp(ln(a) * b)
It may or may not abs() "a" which is where the problems will occur
because the ln of a is not defined.
Tom
On Tue, 22 Feb 2005 15:12:57 +0000, Shaobo Hou
<ho***@cs.man.ac.uk> wrote: Can anyone tell me why pow(8.0, 1.0 / 3.0) (cubic root of 8) returns nan (in linux) and negative infinity or something (in devcpp in windows), instead of 2?
The problem seems to be that pow can't handle cubic root of negative number, I mean, a calculator could do it or am I using the wrong function here. My current solution is a hack which detects negative input and handle it differently.
I suspect it is something to do with the fact the power is represented as a floating point value.
The parameters to pow(x, y) are always floating point. However:
7.12.7.4 The pow functions
2 The pow functions compute x raised to the power y. A domain error
occurs if x is negative and y is finite and not an integer value.
Among other things, 1.0 / 3.0 is not 1/3, it's an approximation, so the
function can't tell that it has a real answer at all (only fractional
powers with an odd integral divisor have real roots, others have complex
roots.
C99 has the function cbrt() which calculates a cube root specifically
(as, I suspect, does your calculator). However, there are few
conforming C99 libraries yet (after all, that specification has only
been out for 6 years!).
In general it is better to give an error if the programmer tries to do
something which has an unrepresentable result.
I would implement my own cube root function, either "from scratch"
(using Newton/Raphson or a better algorithm) or as:
double myCubeRoot(double x)
{
return (x >= 0 ? pow(x, 1.0/3.0) : pow(x, 1.0/3.0));
}
(or as a macro).
Note that since pow() uses exp(log(x)*y) (or a faster equivalent) it
will in general be slower than a specialpurpose cube root function, as
well as being less accurate (see above about binary approcimation to
fractions).
Chris C
Tom St Denis wrote: [...] That's nonsense. pow is normally implemented as
pow(a, b) = exp(ln(a) * b)
(Slight topic drift): For suitable values of "normally,"
with implications of "quick and dirty." This is not a very
accurate implementation of pow(), and I wouldn't expect to
find it in a highquality math library. Reference: "The
Standard C Library" by P.J. Plauger gives a brief but quite
understandable explanation of the problems and exhibits an
implementation that mitigates them.
 Er*********@sun.com
"Shaobo Hou" <ho***@cs.man.ac.uk> wrote in message
news:cv*********@wapping.cs.man.ac.uk... Can anyone tell me why pow(8.0, 1.0 / 3.0) (cubic root of 8) returns nan (in linux) and negative infinity or something (in devcpp in windows), instead of 2?
The problem seems to be that pow can't handle cubic root of negative number, I mean, a calculator could do it or am I using the wrong function here. My current solution is a hack which detects negative input and handle it differently.
I suspect it is something to do with the fact the power is represented as a floating point value.
The first argument has to be positive.
7.12.7.4 The pow functions
Synopsis
1 #include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
Description
The pow functions compute x raised to the power y. *A domain error occurs
if x is finite and negative* and y is finite and not an integer value. A
domain error may occur if x is zero and y is less than or equal to zero. A
range error may occur.
On Tue, 22 Feb 2005 13:38:10 0500, AC
<te**@test.test> wrote: "Shaobo Hou" <ho***@cs.man.ac.uk> wrote in message news:cv*********@wapping.cs.man.ac.uk... Can anyone tell me why pow(8.0, 1.0 / 3.0) (cubic root of 8) returns nan (in linux) and negative infinity or something (in devcpp in windows), instead of 2?
The problem seems to be that pow can't handle cubic root of negative number, I mean, a calculator could do it or am I using the wrong function here. My current solution is a hack which detects negative input and handle it differently.
I suspect it is something to do with the fact the power is represented as a floating point value. The first argument has to be positive.
Or the second argument has to be finite and an integer value. Read
what you quoted:
7.12.7.4 The pow functions
Synopsis
1 #include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
Description
The pow functions compute x raised to the power y. *A domain error occurs if x is finite and negative* and y is finite and not an integer value.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
There's an 'and' clause in there, pow(8.0, 3) is valid, for instance
(the 3 is promoted to a double as long as the prototype is in scope).
Chris C
"Chris Croughton" <ch***@keristor.net> wrote in message
news:sl******************@ccserver.keris.net...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ There's an 'and' clause in there, pow(8.0, 3) is valid, for instance (the 3 is promoted to a double as long as the prototype is in scope).
Chris C
You are right, I should have mentioned that too.
Eric Sosman wrote: Tom St Denis wrote: [...] That's nonsense. pow is normally implemented as
pow(a, b) = exp(ln(a) * b)
(Slight topic drift): For suitable values of "normally," with implications of "quick and dirty." This is not a very accurate implementation of pow(), and I wouldn't expect to find it in a highquality math library. Reference: "The Standard C Library" by P.J. Plauger gives a brief but quite understandable explanation of the problems and exhibits an implementation that mitigates them.
pow(a, b) = exp(ln(a) * b)
is EXACTLY correct.
.... however as implemented [and executed] it may not be ideal. The
"crux" of what the pow function does is probably along the lines of the
equivalence since you can easily find exp and ln from two convergent
series.
Tom
Tom St Denis wrote: Eric Sosman wrote:
Tom St Denis wrote:
[...] That's nonsense. pow is normally implemented as
pow(a, b) = exp(ln(a) * b) (Slight topic drift): For suitable values of "normally," with implications of "quick and dirty." This is not a very accurate implementation of pow(), and I wouldn't expect to find it in a highquality math library. Reference: "The Standard C Library" by P.J. Plauger gives a brief but quite understandable explanation of the problems and exhibits an implementation that mitigates them.
pow(a, b) = exp(ln(a) * b)
is EXACTLY correct.
My apologies; I'd assumed that because you said
"implemented as" you intended this as a C expression
(with `log' misspelled) rather than as a mathematical
expression.
... however as implemented [and executed] it may not be ideal.
This was the point of my post: Interpreted as a C
expression, exp(log(a) * b) is a poor way to implement
pow(a,b).
 Er*********@sun.com This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by berthelot samuel 
last post: by

52 posts
views
Thread by Michel Rouzic 
last post: by

11 posts
views
Thread by Russ 
last post: by

42 posts
views
Thread by John Smith 
last post: by

2 posts
views
Thread by merrittr 
last post: by

18 posts
views
Thread by Peng Yu 
last post: by
          