help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What does power operator ^ returns?


From: Jaroslav Hajek
Subject: Re: What does power operator ^ returns?
Date: Mon, 19 Oct 2009 21:58:08 +0200

On Mon, Oct 19, 2009 at 6:42 PM, Guido Walter Pettinari
<address@hidden> wrote:
> Hi all!
>
> I need to compute the cubic root of many negative numbers. My problem
> is real-valued, hence I am not interested in the imaginary solutions.
> However, if I use the power operator '^', Octave returns just the
> first imaginary solution. The same applies when doing any odd root.
>
> Example:
> octave> a=-8
> a = -8
> octave> a^(1/3)
> ans = 1.000000000000000e+00 + 1.732050807568877e+00i
>
> How can I make octave to return the real solution, which in my example
> is just -2? I do not want just the absolute value (i.e. abs(a^(1/3)),
> since it does not preserve the sign.  I could do with a check on the
> sign, but it would be inefficient.
>
> I am using Octave 3.2.3 on Mac Os X 10.6.
>
> Thank you very much,
>
> Guido

What you get is the standard complex power. The problem here is
consistency. In fact, 1/3 is not exactly representable, so -8^(1/3) is
not actually a cube root. We could optimize the x^(1/y) syntax so that
the expected thing happens if y is an odd integer, but that would make
x^(1/y) discontinuous w.r.t. y, so I don't think it's a good idea.
The nthroot function should calculate a real root; however, currently
it's an m-file implementation and is not more efficient than what you
can do yourself. An optimized compiled implementation would no doubt
be welcome.

regards

-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]