Cube root of a negative number

Dear rooters

Please could you explain the following:

root [0] TMath::Power(-8,0.3333)
Error: Symbol nan is not defined in current scope :0:
(Double_t)nan
root [1] TMath::Power(8,(1/3))
(Double_t)1.00000000000000000e+00
root [2] TMath::Power(-8,(1/3))
(Double_t)1.00000000000000000e+00

None of these answers are correct. Am I doing something completely stupid?

Thanks in advance.
Alan.

I have checked this on the following systems, and both have the same symptom:

MacOSX 10.4.7 PPC
8.7.0 Darwin Kernel Version 8.7.0: Fri May 26 15:20:53 PDT 2006; root:xnu-792.6.76.obj~1/RELEASE_PPC Power Macintosh powerpc
Root Version 5.11/06 1 June 2006
gcc version 4.1.0 20051124 (prerelease)

Suse 10.0 Intel EM64
Linux 2.6.13-15.8-smp x86_64 GNU/Linux
Root Version 5.10/00 1 March 2006
gcc version 4.0.2 20050901 (prerelease) (SUSE Linux) Configured with: …/configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,f95,java,ada --disable-checking --with-gxx-include-dir=/usr/include/c++/4.0.2 --enable-java-awt=gtk --disable-libjava-multilib --with-slibdir=/lib64 --with-system-zlib --enable-shared --enable-__cxa_atexit --without-system-libunwind --host=x86_64-suse-linux

  1. 0.3333 != 1/3.
  2. Expression 1/3 has two operands of type int, int / int has type int, 1 / 3 == 0.
    8^0 == 1.
  3. (-8)^0 == 1.

Thanks for the reply - the final two now make sense (and I was being stupid).

However, it doesn’t answer the first one, as the cube root of -8 is -2:

root [2] TMath::Power(-8.0,static_cast(1)/static_cast(3))
Error: Symbol nan is not defined in current scope :0:
(Double_t)nan
*** Interpreter error recovered ***

Am I being stupid again?

[quote]
root [2] TMath::Power(-8.0,static_cast(1)/static_cast(3))
Error: Symbol nan is not defined in current scope :0:
(Double_t)nan
*** Interpreter error recovered ***
Am I being stupid again?[/quote]

Casts will not help you :slight_smile: 1.f / 3.f (or 1./3.) is a floating point number, which != mathematical 1/3. You need a LISP or something like this :slight_smile: (it has a special type for ratio) :slight_smile:. But in C++ you can simply take TMath::Power(8, 0.3333), the result will be near 2 and you know, it must be negative :slight_smile:

Hi,

as you cans see in root.cern.ch/root/html/TMath#TMath:Power it just calls pow. “man pow” states for me:

[quote=“man pow”]The pow() function can return the following error:
EDOM The argument x is negative and y is not an integral value. This would result in a complex number.[/quote] I disagree with their definition of complex numbers, but we’ll have to live with that.

Cheers, Axel.

Ugh… I see the problem. Thanks - I now have a rather inelegant if statement.

[quote=“Axel”]Hi,

as you cans see in root.cern.ch/root/html/TMath#TMath:Power it just calls pow. “man pow” states for me:

[quote=“man pow”]The pow() function can return the following error:
EDOM The argument x is negative and y is not an integral value. This would result in a complex number.[/quote] I disagree with their definition of complex numbers, but we’ll have to live with that.
[/quote]

When ‘x is negative and y is not an integral value’, you can always come up with complex roots (as well as possibly a real one as well).

In any case, if you want ‘cuberoot (-27.)’ to give you ‘-3’, you’ll want to keep track of the sign of the argument and appy it after

double cuberoot (x)
{
   return  (x / fabs (x)) * pow ( fabs(x), 1/3. );
}

Hi Charles,
I’d suggest a new func name:

double cuberoot_for_all_but_zero (x)
{
   return  (x / fabs (x)) * pow ( fabs(x), 1/3. );
}

:wink:

Cheers, Axel.

Actually, this is what I should have done

double cuberoot (double x)
{
   if (! x) return 0.;
   return  (x / fabs (x)) * pow ( fabs(x), 1/3. );
} 

Then I can keep my name :slight_smile: