Showing posts with label DM15. Show all posts
Showing posts with label DM15. Show all posts

Saturday, August 9, 2025

RPN: HP 15C and DM32: Integer Length and Digit Extraction

RPN: HP 15C and DM32: Integer Length and Digit Extraction (up to 99,999,999)



The following programs are utilities for positive integers:


(1) Integer Length: how many digits in a positive integer

(2) Digit Extraction: Get the nth digit of a positive integer


Note:  Do not use more than 8 digits (integers up to 99,999,999).   Beyond this, the program may return inaccurate answers.  

Thanks to J-F Garnier for pointing out the limitation of the programs.  


Integer Length


Find the number of digits of a positive integer. The integer is decimal base (base 10).


For a positive integer n, the number of the digits can be easily found by the formula:


L = int(log(x)) + 1



DM32, HP 32S, HP 32SII Code


L01 LBL L

L02 LOG

L03 IP

L04 1

L05 +

L06 RTN


HP 15C, DM15 Code


001

42, 21, 11

LBL A

002

43, 13

LOG

003

43, 44

INT

004

1

1

005

40

+

006

43, 32

RTN


The program finds the number of digits in the positive integer in the X stack.


Examples:


X = 436782; Length: 6

X = 5195008; Length: 7

X = 23156956; Length: 8


Digit Extraction


Extract the nth digit of a positive integer. Digit positions go from left to right. For example: for the integer 4582, the 1st digit is 4, the 2nd digit is 5, 3rd digit is 8, and 4th digit is 2.


Steps that this program follows:

Step 1: Find the length of the positive integer. L = int(log(x)) + 1.

Step 2: Divide X by 10^(L – n + 1). D = X / (10^(L – n + 1))

Step 3: Extract the fractional part. D = frac(D)

Step 4: Multiple the result by 10 and extract the integer part. D = int(10 * D)


Setting up the stack:

Y: integer

X: nth digit to extract


Variables Used:

DM32, HP 32S, HP 32S II

HP 15C, DM15

X

R1

N

R2

L

R3



DM32, HP 32S, HP 32SII Code


E01 LBL E

E02 STO N

E03 R↓

E04 STO X

E05 LOG

E06 IP

E07 1

E08 +

E09 STO L

E10 RCL X

E11 RCL L

E12 RCL- N

E13 1

E14 +

E15 10^x

E16 ÷

E17 FP

E18 10

E19 ×

E20 IP

E21 RTN


HP 15C, DM15 Code


001

42, 21, 12

LBL B

002

44, 2

STO 2

003

33

R↓

004

44, 1

STO 1

005

43, 13

LOG

006

43, 44

INT

007

1

1

008

40

+

009

44, 3

STO 3

010

45, 1

RCL 1

011

45, 3

RCL 3

012

45, 30, 2

RCL- 2

013

1

1

014

40

+

015

13

10^x

016

10

÷

017

42, 44

FRAC

018

1

1

019

0

0

020

20

×

021

43, 44

INT

022

43, 32

RTN


Examples


Integers

Length

2nd Digit

4th Digit

5th Digit

436782

6

3

7

8

5195008

7

1

5

0

23156956

8

3

5

6



Eddie


All original content copyright, © 2011-2025. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.


All posts are 100% generated by human effort.  The author does not use AI engines and never will.


Saturday, May 10, 2025

RPN with HP 15C and DM32: Complex Mode

RPN with HP 15C and DM32: Complex Mode



HP 15C Complex Mode


This section also applies to the Swiss Micros DM15 family.


Complex mode is a separate mode for the HP 15C. Complex mode is turned on by setting flag 8. Complex mode is on when there is a “C” indicator in the display.


In complex mode, the stack is expanded. Each stack level now includes a real part and a complex part. What is in the display is always the real part.


Stack T

Real part of T

Imaginary part of T

Stack Z

Real part of Z

Imaginary part of Z

Stack Y

Real part of Y

Imaginary part of Y

Stack X

Real part of X

Imaginary part of X


The great news is that the stack handles up to four complex numbers, one for each of the stack levels.


To display the imaginary part, we have two methods:


Temporary View: Press [ f ] and hold [ COS ] (i) to temporarily view the complex part. The stack remains unaffected.


Switch: Switch the real part and the imaginary part by pressing [ f ] [ - ] (Re<>Im). Doing this switches the real and imaginary parts of the X stack.


For example: X = 9 – 8i


Enter the complex number as such: 9 [ ENTER ] 8 [ CHS ] [ I ] * (see note).

The displays shows 9.

Press [ f ] [ - ] (Re<>Im). The display shows -8 and the X stack now has the complex number X = -8 + 9i.

Press [ f ] [ - ] (Re<>Im) to switch the parts back to the original complex number.


* Note: If complex mode is turned off (no C indicator), using this key sequence sets flag 8, turning on complex mode automatically.


The switching of parts is important, because memory registers can not hold an entire complex number, but it’s separate parts. Thus, we will need two registers, one for the real part and one for the imaginary part.



HP 15C: Leaving Complex Mode


To leave complex mode, clear flag 8 (CF 8). The imaginary parts of the stack are lost.



Angle Mode


In complex mode, the trigonometric functions operate as the angles are always in radian measure regardless of the angle setting. The only functions that recognize the angle setting are the polar/rectangular conversions. To take the cosine and sine of angle that respects the conversion, use the following sequence:


angle [ENTER] 1 [ f ] [ →R ]: X stack: cos(angle), Y stack: sin(angle)



HP 15C - Solving Monic Quadratic Equations


The HP 15C’s solver only works for real numbers, so manual methods and formulas must be used to solve equations for complex numbers.



Z^2 + w1 * Z + w0 = 0


Solution: Z = (w1 ± √(w1^2 – 4 * w0)) / 2

(D = √(w1^2 – 4 * w0), Z+ = (w1 + D) / 2 Z- = Z+ - D)


Store the following:


Complex coefficient w1: Real part in register 4, Imaginary part in register 5

Complex coefficient w2: Real part in register 1, Imaginary part in register 2


The results are stored in the following registers:


Discriminant (D): Real part in register 6, imaginary part in register 3

Complex root Z+: Real part in register .0 (decimal point-0), imaginary party in register 8

Complex root Z-: Real part in register 9, imaginary part in register 7


Code (use any label you want, I use label A for example):


Key

Key Code


Key

Key Code

LBL A

42, 21, 11


RCL 4

45, 4

SF 8

43, 4, 8


RCL 1

45, 1

RCL 4

45, 4


I

42, 25

RCL 1

45, 1


-

30

I

42, 25


2

2

x^2

43, 11


÷

10

RCL 5

45, 5


STO .0

44, .0

RCL 2

45, 2


Re<>Im

42, 30

I

42, 25


STO 8

44, 8

4

4


Re<>Im

42, 30

×

20


R/S

31

-

30


RCL 6

45, 6

√

11


RCL 3

45, 3

STO 6

44, 6


I

42, 25

Re<>Im

42, 30


-

30

STO 3

44, 3


STO 9

44, 9

Re<>Im

42, 30


Re<>Im

42, 30




STO 7

44, 7




Re<>Im

42, 30




RTN

43, 32



Example:

Z^2 + (4 + i) * Z + (2 - 5i) = 0

w1: 4 STO 4, 1 STO 1

w2: 2 STO 5, 5 CHS STO 2


[ f ] A or [ GSB ] A:

0.11724 [ f ] hold (i) 1.15309 (Z+ ≈ 0.11724 + 1.15309i)

[ R/S ]

-4.11724 [ f ] hold (i) -2.15309 (Z- ≈ -4.11724 – 2.15309i)



DM32 Complex Mode


This section also applies to the HP 32Sii, HP 32S, and the HP 41C/DM41X with the Advantage ROM plugged in. The names of the functions vary.


There is no “separate” complex mode for the DM32, all the functions are access with shifted CMPLX prefix function. The complex number functions available on the DM32 are:


CMPLX+, CMPLX-, CMPLX×, CMPLX÷

CMPLX+/- (change sign, multiply the complex number by -1)

CMPLX1/x, CMPLXe^x, CMPLXLN, CMPLXy^x

CMPLXSIN, CMPLXCOS, CMPLXTAN


The complex functions grabs the values from the four stack levels and uses them as up to two complex numbers:

Z

Imaginary part of T + Zi

T

Real part of T + Zi

Y

Imaginary part of X + Yi

X

Real part of X + Yi


Memory registers can not hold an entire complex number, but it’s separate parts. Thus, we will need two registers, one for the real part and one for the imaginary part.


To enter complex numbers, enter the imaginary part, press [ ENTER ], then enter the real part.



Square Root and Square (√ and x^2)


There is no complex square root or complex square function. We will need some creativity to tackle these functions. Here is just one way we can accomplish this task.


Assume the complex number A + Bi have the real part stored in A and imaginary part stored in B.


Square Root (√):

RCL B

RCL A

0.5

ENTER

Clx

x<>y

CMPLXy^x


Square (x^2):

RCL B

RCL A

RCL B

RCL A

CMPLX×


Angle Mode


In complex mode, the trigonometric functions operate as the angles are always in radian measure regardless of the angle setting. The only functions that recognize the angle setting are the polar/rectangular conversions. To take the cosine and sine of angle that respects the conversion, use the following sequence:


1 [ENTER] angle [ blue shift ] [ →y,x ]: X stack: cos(angle), Y stack: sin(angle)


(HP 32SII late editions: lavender/purple shift)



DM32 - Solving Monic Quadratic Equations


Like the HP 15C, the DM32’s solver only works for real numbers, so manual methods and formulas must be used to solve equations for complex numbers.


Z^2 + w1 * Z + w0 = 0


Solution: Z = (w1 ± √(w1^2 – 4 * w0)) / 2

(D = √(w1^2 – 4 * w0), Z+ = (w1 + D) / 2 Z- = Z+ - D)


Store the following:


Complex coefficient w1: Real part in A, Imaginary part in B

Complex coefficient w2: Real part in C, Imaginary part in D

The program will prompt for A, B, C, and D, in the order of imaginary part, then real part

Z^2 + (Bi + A) * Z + (Di + C) = 0


The results are stored in the following registers:


Discriminant (D): Real part in E, imaginary part in F

Complex root Z+: Real part in R, imaginary party in S

Complex root Z-: Real part in U, imaginary part in V


Code:

A01 LBL A

A24 x<>y

A02 INPUT B

A25 RCL B

A03 INPUT A

A26 RCL A

A04 INPUT D

A27 CMPLX-

A05 INPUT C

A28 2

A06 RCL B

A29 ENTER

A07 RCL A

A30 CLx

A08 RCL B

A31 x<>y

A09 RCL A

A32 CMPLX÷

A10 CMPLX×

A33 STO S

A11 4

A34 x<>y

A12 RCL× D

A35 STO R

A13 4

A36 x<>y

A14 RCL× C

A37 STOP

A15 CMPLX-

A38 RCL F

A16 0.5

A39 RCL E

A17 ENTER

A40 CMPLX-

A18 CLx

A41 STO U

A19 x<>y

A42 x<>y

A20 CMPLXy^x

A43 STO V

A21 STO E

A44 x<>y

A22 x<>y

A45 RTN

A23 STO F




Example:

Z^2 + (4 + i) * Z + (2 - 5i) = 0

[ XEQ ] A

B? 1 [R/S]

A? 4 [R/S]

D? -5 (5 [+/-] ) [R/S]

C? 2 [R/S]


Y: 1.15309, X: 0.11724 (Z+ ≈ 0.11724 + 1.15309i)

[R/S]

Y: -2.15309, X: -4.11724 (Z- ≈ -4.11724 – 2.15309i)




I hope you enjoyed this edition of RPN with HP 15C and DM2,


Eddie


All original content copyright, © 2011-2025. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Python in Numworks: Duplicating and Grayscale

Python in Numworks: Duplicating and Grayscale All three scripts presented today use the math, random, and the Numworks specific ...