Post Reply 
Free42 and cube root of complex number
02-23-2021, 11:05 PM
Post: #1
Free42 and cube root of complex number
Why the result is different?
Code:

                8  ENTER
               0 COMPLEX
                    8 i0
                     +/-
                   -8 i0
                3    1/X
 3.33333333333ᴇ-1    ***
                     Y↑X
       1 -i1.73205080757
Code:

                  -8  ENTER
               0 COMPLEX
                   -8 i0
                3    1/X
 3.33333333333ᴇ-1    ***
                     Y↑X
        1 i1.73205080757
Find all posts by this user
Quote this message in a reply
02-24-2021, 02:02 AM
Post: #2
RE: Free42 and cube root of complex number
In the first case, the imaginary part is -0, while in the second case, it is 0.
-0, also known as signed zero, is a feature of IEEE-754 floating point. The difference between 0 and -0 shouldn't matter in Free42, but apparently this is a case where it does and where there is no special-case code for zero to prevent this behavior.

What happens internally is that the number is first converted to polar using the hypot() and atan2() functions, and atan2(-0, -8) => -pi, while atan2(0, -8) => pi. After multiplying that angle by 1/3, you end up on opposite sides of the real axis.

I'll fix this in the next release.

(You won't see this behavior in ->POL, because it does contain special-case code for handling zero in either coordinate.)
Visit this user's website Find all posts by this user
Quote this message in a reply
02-24-2021, 02:52 AM
Post: #3
RE: Free42 and cube root of complex number
From a numerical viewpoint, the current behaviour is correct.
Find all posts by this user
Quote this message in a reply
02-24-2021, 08:28 AM (This post was last modified: 02-24-2021 12:33 PM by J-F Garnier.)
Post: #4
RE: Free42 and cube root of complex number
The 1984 HP-71 was supporting the signed zero, but it was sometimes a source of confusion for users and the signed zero was not kept in later machines (starting with the 28C).

So yes, the HP-71, with the Math ROM, gives the same (correct) results:
> (-8,0) ^ (1/3)
(1,1.73205080757)
> (-(8,0)) ^ (1/3)
(1,-1.73205080757)


J-F
Visit this user's website Find all posts by this user
Quote this message in a reply
02-24-2021, 08:59 AM
Post: #5
RE: Free42 and cube root of complex number
Yes, the sign of zero is important!
Read Kahan's 'How Java’s Floating-Point Hurts Everyone Everywhere'.
Please, leave it as it is - Free42 would be one of the only ones to get it right ;-)
Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
02-24-2021, 10:26 AM
Post: #6
RE: Free42 and cube root of complex number
(02-24-2021 02:02 AM)Thomas Okken Wrote:  atan2(-0, -8) => -pi, while atan2(0, -8) => pi

-pi < arg(z) ≤ pi

Since polar form and rectangular form should match, I think -8 ± 0i should be adjusted to -8 + 0i = 8 exp(i*pi)

For the same reason, sqrt(-4 - 0i) = sqrt(-4 + 0i) = 2i

4 [ENTER] 0 [COMPLEX] [+/-] [SQRT]       → 2i
Find all posts by this user
Quote this message in a reply
02-24-2021, 01:35 PM
Post: #7
RE: Free42 and cube root of complex number
(02-24-2021 08:59 AM)Werner Wrote:  Please, leave it as it is - Free42 would be one of the only ones to get it right ;-)

There is that! But the HP-42S doesn't support -0, and when I did the review of complex transcendentals a few years ago, I added special cases for pure-real and pure-imaginary numbers that made the not-42S-compatible behavior with -0 disappear.

That review only covered the transcendentals, though. I missed SQRT, for which I ended up creating a special case for pure-imaginary a bit later. And this issue with Y^X follows the same pattern.
Visit this user's website Find all posts by this user
Quote this message in a reply
02-24-2021, 02:47 PM
Post: #8
RE: Free42 and cube root of complex number
(02-24-2021 01:35 PM)Thomas Okken Wrote:  
(02-24-2021 08:59 AM)Werner Wrote:  Please, leave it as it is - Free42 would be one of the only ones to get it right ;-)

There is that! But the HP-42S doesn't support -0 ...

Hi, Thomas Okken

Can you elaborate what is decided ?

If Free42 do support -0, then sqrt(-4-0i) = sqrt(4*exp(-pi*i)) = 2*exp(-pi/2*i) = -2i

But, user need to know "-0" is actually inputed.
-0 should displayed as "-0", not "0"
-4-0*i should displayed as "-4 -i0", not "-4 i0"

If not, (-8-0i)^(1/3) = (-8+0i)^(1/3) = 1 + sqrt(3)*i
Find all posts by this user
Quote this message in a reply
02-24-2021, 02:53 PM
Post: #9
RE: Free42 and cube root of complex number
(02-24-2021 02:47 PM)Albert Chan Wrote:  Can you elaborate what is decided ?

The behavior reported by Ajaja is a bug. Free42 does not support -0, and so the two calculations he showed should return the same result.

The only case where the sign of zero actually matters in Free42 is when it gets passed as the first parameter to atan2(). That scenario is supposed to be prevented by special-case logic, but that check is missing in Y^X. I'll fix that in the next release.
Visit this user's website Find all posts by this user
Quote this message in a reply
02-25-2021, 06:08 AM
Post: #10
RE: Free42 and cube root of complex number
.. and, not unimportant, that way it is consistent with the 42S.
Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
02-25-2021, 04:40 PM
Post: #11
RE: Free42 and cube root of complex number
FYI, we can remove -0 parts of x, by doing x-x+x = 0+x
(assumed 0+x does not get optimized away, to plain x)

Redo OP example:

8 [Enter] 0 [Complex] [+/-] → x = -8 - 0i
[Enter] - [LastX] +              → x-x+x = -8 + 0i
3 [1/x] [y^x]                      → ≈ 1 + 1.73205080757*i
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 3 Guest(s)