(42S) Argument, Real Part, Imaginary Part, and Sign Function
12-29-2018, 05:25 AM
Post: #1
 Eddie W. Shore Senior Member Posts: 1,249 Joined: Dec 2013
(42S) Argument, Real Part, Imaginary Part, and Sign Function
The HP 42S* has only one extraction function for complex numbers: ABS (absolute value) by default. To complete the list, here are some programs for ARG (argument), REAL (real part), and IMAG (imaginary part). The three functions take a complex number, in either rectangular or polar form.

Using stack commands and flag checks, I am able to get obtain results without affecting the stack much (although the z and t stacks will have the same value).

Also included is the SIGN (signum) function.

* This applies to the Swiss Micros DM42 (it should work), Free42 from Thomas Oakken, and any other HP 42S emulator apps. The print out is from Free42 (and I also have a physical HP 42S).

Disclaimer: I don't own a DM42... yet.

HP 42S Program: ARG
Code:
 00 { 19-Byte Prgm } 01▸LBL "ARG" 02 COMPLEX 03 X<>Y 04 FC? 73 05 →POL 06 R↑ 07 STO ST Y 08 R↓ 09 R↓ 10 RTN 11 .END.

HP 42S Program: REAL
Code:
 00 { 21-Byte Prgm } 01▸LBL "REAL" 02 COMPLEX 03 X<>Y 04 FS? 73 05 →REC 06 X<>Y 07 R↑ 08 STO ST Y 09 R↓ 10 R↓ 11 RTN 12 END

HP 42S Program: IMAG
Code:
 00 { 20-Byte Prgm } 01▸LBL "IMAG" 02 COMPLEX 03 X<>Y 04 FS? 73 05 →REC 06 R↑ 07 STO ST Y 08 R↓ 09 R↓ 10 RTN 11 END

HP 42S Program: SIGN
Code:
 00 { 15-Byte Prgm } 01▸LBL "SIGN" 02 X=0? 03 RTN 04 ENTER 05 ABS 06 X<>Y 07 ÷ 08 RTN 09 END

12-29-2018, 05:48 AM
Post: #2
 Paul Dale Senior Member Posts: 1,733 Joined: Dec 2013
RE: (42S) Argument, Real Part, Imaginary Part, and Sign Function

Code:
LBL "SIGN" x=0? STO L x=0? RTN ABS x<> L RCL/ L RTN

Which (I think, I've no 42 to hand) sets LastX properly and doesn't impact the stack.

Pauli
12-29-2018, 06:35 AM
Post: #3
 Thomas Klemm Senior Member Posts: 1,447 Joined: Dec 2013
RE: (42S) Argument, Real Part, Imaginary Part, and Sign Function
(12-29-2018 05:25 AM)Eddie W. Shore Wrote:  The HP 42S* has only one extraction function for complex numbers: ABS (absolute value) by default.

(…)

Also included is the SIGN (signum) function.

What about using the built-in SIGN function?
12-29-2018, 08:50 AM
Post: #4
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: (42S) Argument, Real Part, Imaginary Part, and Sign Function
(12-29-2018 05:48 AM)Paul Dale Wrote:  What about:

Code:
LBL "SIGN" x=0? STO L x=0? RTN ABS x<> L RCL/ L RTN

This would work if (!) the 42s allowed tests for complex numbers. ;-)
But it throws an "Invalid Type" error here.

(12-29-2018 06:35 AM)Thomas Klemm Wrote:  What about using the built-in SIGN function?

Yes, a complex sign function is not required, the built-in function works nicely. For complex arguments it even returns 0 for 0+0i. For real arguments sign(0) is returned as 1. Maybe for compatibility with the HP-41 which does it the same way. Here SIGN returns 0 only for Alpha data (!).

Dieter
12-30-2018, 07:12 PM
Post: #5
 Gerald H Senior Member Posts: 1,459 Joined: May 2014
RE: (42S) Argument, Real Part, Imaginary Part, and Sign Function
On my real HP 42S

SIGN

returns

0+0i

for input

0+0i
12-30-2018, 08:19 PM (This post was last modified: 12-30-2018 08:20 PM by Dieter.)
Post: #6
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: (42S) Argument, Real Part, Imaginary Part, and Sign Function
(12-30-2018 07:12 PM)Gerald H Wrote:  [ SIGN returns 0+0i for input 0+0i ]

Same for Free42. For complex arguments SIGN always returns a complex result.
Sorry, my description "...it even returns 0 for 0+0i" was inaccurate.

Dieter
 « Next Oldest | Next Newest »

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