Well, mathematically, say,  (-27)^(1/3) = 3rd root of -27, which is equal to -3.

But your example below clarifies that my approach with the use of the sign function (or Sven's conditional statement) are applicable only when we have as power a rational formed by two odd numbers: 1/3,  3/7, etc.

In such cases, with a negative base the solution will be real-valued and it will preserve the minus sign of the base.

Certainly, these are special cases, but they are the cases for which a real-valued solution exists, which is why the "domain error" message drew my attention.

Alecos Papadopoulos PhD
Athens University of Economics and Business
web: alecospapadopoulos.wordpress.com/
On 13/8/2020 04:55, Allin Cottrell wrote:
On Wed, 12 Aug 2020, Alecos Papadopoulos wrote:

Now, consider series "Z" which has both positive /and/ negative values. We want to compute

W = Z^(1/3)

Given a recent new feature in gretl, the sign function sgn(), I guess we can avoid the obvious conditional if-else statement, and compute W by writing

W = sgn(Z) * (abs(Z)^(1/3))

that covers also the case of Z taking an exact zero value.

Is this the proper/efficient way?

My second reply. No, this is not proper!

? Z = -2
Generated scalar Z = -2
? W = sgn(Z) * (abs(Z)^(1.0/4))
Generated scalar W = -1.18921
? eval W^4

octave:1> Z = -2
Z = -2
octave:2> W = Z^(1.0/4)
W =  0.84090 + 0.84090i
octave:3> W^4
ans = -2.0000e+00 + 1.2561e-15i

There's a digital approximation involved in the octave answer, via complex numbers, but it's basically right, while the answer according to your proposal is flat wrong.


Gretl-users mailing list -- gretl-users@gretlml.univpm.it
To unsubscribe send an email to gretl-users-leave@gretlml.univpm.it
Website: https://gretlml.univpm.it/postorius/lists/gretl-users.gretlml.univpm.it/