Showing posts with label registers. Show all posts
Showing posts with label registers. Show all posts

Saturday, May 13, 2023

HP 12C Using TVM variables as Registers

 HP 12C Using TVM variables as Registers



Special Thanks

Regarding the HP 12C Tips talk I gave on HHC 2017, I received this tip from an email from   SergioSamy.   Appreciation and gratitude.  


To watch my HP 12C tip presentation, click here:

https://www.youtube.com/watch?v=Q54UbDzpAIw




Why Use TVM Variables?


Normally, we use the variables n, i, PV, PMT, and FV for time value of money, cash flow, and depreciation calculations.  They can be used for variables for non-finance or other business calculations as well.


When we program on the Classic HP 12C, we have 99 steps.  If the program memory is clear, we have 20 registers from R.9  (register dot-nine) to R0 (register zero).   For every eight steps, a memory register is lost.   


At full capacity of the Classic HP 12C, we only have seven registers left (R0 through R6).  


The moment that we use press the [ Σ+ ] button and use statistics, the registers R1 and R6 get occupied, possibly leaving R0 to use for other purposes:


R1 = n

R2 = Σx

R3 = Σx^2

R4 = Σy

R5 = Σy^2

R6 = Σxy


However, the TVM registers: n, i, PV, PMT, and FV, are permanent memory registers for use.   Today's programs will illustrate this use.   


Note:  Pressing the TVM keys once after a number (key word: after) stores the number in a variable.   If you don't key a number first, then the program solves for the a variable.  Let's look at a couple of examples.



Economic Ordering Quantity


Formula:


EOQ = √(2 * CPO * SLS ÷ (CPU * HOLD%))


CPO = fixed order cost

SLS = number of project sales units

CPU = variable cost per unit

HOLD% = projected cost of holding inventory

EOQ = economic ordering quantity, in units


TVM variables used:


[ n ]:  EOQ, computed

[ i ]: HOLD%,  entered

[ PV ]:  CPU, entered

[ PMT ]:  CPO, entered

[ FV ]:  SLS, entered



Program Code:


Line Number:  Key Code;  Key


01:  45, 14;  RCL PMT

02:  45, 15;  RCL FV

03:  20;  ×

04:  2;  2

05:  20;  ×

06:  45, 13;  RCL PV

07:  45, 12;  RCL i

08:  25;  %

09:  34;  x<>y

10:  33;  R↓

11:  10;  ÷

12:  43, 21;  √

13:  44, 11;  STO n

14:  43,33,00;  GTO 00



Example


CPO = 40.95

SLS = 8,000

CPU = 5.50

HOLD% = 10%


10 [ i ]

5.50 [ PV ]

40.95  [ PMT ]

8000 [ FV ]

[ R/S ].  Result:  1091.45


Source


HP-17B, HP-19B, HP-27S  Step-by-Step Solutions For Your HP Calculator.  Marketing and Sales.   Hewlett Packard.  Edition 1.  January 1988



Quadratic Equation


Instructions:


Find the roots of a*x^2 + b*x + c = 0


The program ends with the discriminant stored in R0. 


D = b^2 - 4*a*c


If D<0, the roots are complex in the form of s ± ti:

s:  real part, stored in PMT

t:  imaginary part, stored in FV


If D≥0, the roots are real and:

Root 1 is stored in PMT

Root 2 is stored in FV


Store a in [ n ], b in [ i ], and c [ PV ].  Then run the program. 


Note:  to get the absolute value, square then take the square root of the number.  This is why I have two square root commands in a row: one is to make the absolute value; the second to take the square root of D. 


Program Code:


Line Number:  Key Code;  Key


01:  45,12;  RCL I

02:  2;  2

03:  21;  y^x

04:  45, 13;  RCL PV

05:  45, 11;  RCL n

06:  20;  ×

07:  4;  4

08:  20;  ×

09:  30;  -

10:  44, 0;  STO 0

11:  45, 12;  RCL i

12:  16;  CHS

13:  2;  2

14:  10;  ÷

15:  45, 11;  RCL n

16:  10;  ÷

17:  44, 14;  STO PMT

18:  45, 0;  RCL 0

19:  2;  2

20:  21;  y^x

21:  43, 21;  √

22:  43, 21;  √

23:  2;  2

24:  10;  ÷

25:  45, 11;  RCL n

26:  10;  ÷

27:  44, 15;  STO FV

28:  0;  0

29:  45, 0;  RCL 0

30:  43, 34;  x≤y

31:  43,33,00;  GTO 00

32:  45,14;  RCL PMT

33:  36;  ENTER

34:  36;  ENTER

35:  45, 15;  RCL FV

36:  30;  -

37:  44, 15; RCL FV

38:  33; R↓

39:  43, 36;  LSTx

40:  40;  +

41:  44,14; STO PMT

42:  45, 0;  RCL 0

43:  43,33,00;  GTO 00


Examples


2*x^2 - 3*x + 6 = 0


2 [ n ]

-3 [ i ] 

6 [ PV ]

[ R/S ]


Results:

D = -39.00   (complex roots)

[ RCL ] [ PMT ]:  Real part:  0.75

[ RCL ] [ FV ]:  Imaginary part:  1.56

0.75 ± 1.56i


x^2 + 8*x - 4 = 0


1 [ n ]

8 [ i ] 

-4 [ PV ]

[ R/S ]


Results:

D = 80

[ RCL ] [ PMT ]:  Root 1:  0.47

[ RCL ] [ FV ]:  Root 2:  -8.47

0.47, -8.47



Hope you find this tip helpful,


Eddie 



All original content copyright, © 2011-2023.  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. 


Sunday, December 11, 2022

HP 32SII: Random Number Utilities

HP 32SII:  Random Number Utilities



Note:  The following programs should work with the original HP 32S.   The checksums listed are for the HP 32SII only.  



HP 32SII Program:  Fill a Set of Registers with Increasing Integers


This program fills up the alpha registers with consecutive increasing integers.  Up to 25 slots are available, A- Y, with the lowest being stored in A.   Register Z is used to store the difference.  


The difference from both numbers should not be more than 24.  


F01  LBL F

F02  x<>y

F03  1

F04  -

F05 STO Z

F06 -

F07 STO i

I01  LBL I

I02  RCL i

I03  IP

I04  RCL+ Z

I05  STO(i)

I06  DSE i

I07  GTO I

I08  RTN


F:  10.5 bytes, 32SII checksum 9FB9

I:  12.0 bytes, 32SII checksum E189

Total:  22.5 bytes


Examples


18 to 27:   18 ENTER 27 XEQ F

Results:

A = 18, B = 19, C = 20, D = 21, E = 22, 

F = 23, G = 24, H = 25, I = 26, J = 27



HP 32SII Program:  Pick from Registers A through Y


Enter the number of registers to pick from into register Z.  For example, if you have registers A through J loaded, store 10 into register Z.


Note:  When used for indirect addressing, the absolute value of integer of register i is used.   This will allows us to save a few steps.


G01  RANDOM

G02  RCL× Z

G03  1

G04  +

G05  STO i

G06  RCL(i)

G07  RTN


G:  12.0 bytes, 32SII checksum FE18


Example:


Use the data from the last problem.  


10 STO Z, 

XEQ G:   22

XEQ G:   21

(results will vary)




Source for the next two programs:


Chamberlain, Gary.  "HP-29C Random Number Generators"  PPC Journal V6 N7 October 1979


HP 32SII Program:  Uniform Distribution 


This program picks random numbers between two parameters.


U01  LBL U

U02  STO C

U03  -

U04  STO D

W01  LBL W

W02  RANDOM

W03  RCL× D

W04  RCL+ C

W05  STOP

W06  GTO W


U:  6.0  bytes, 32SII checksum 8022

W: 9.0  bytes, 32SII checksum 88EE

Total:  15.0 bytes


Example:


Random numbers between -1 and 1:


XEQ U:  -0.74076 (press R/S for more random numbers)

0.28000

-0.80090

0.20653

0.50614



HP 32SII Program:  Gaussian Distribution of Random Numbers


Generate random numbers that fit on a normal distribution with mean M and deviation D.  Two random numbers are generated, press x<>y to see both results.


X01  LBL X

X02  INPUT M

X03  INPUT D

X04  RAD   (radians mode)

Y01  LBL Y

Y02  RANDOM 

Y03  2 

Y04  ×

Y05  π

Y06  ×

Y07  RANDOM

Y08  LN

Y09  -2

Y10  ×

Y11 SQRT

Y12  θ,r→y,x

Y13  RCL× D

Y14  RCL+ M

Y15  x<>y

Y16  RCL× D

Y17  RCL+ M

Y18  STOP

Y19  GTO Y


Example:


Mean = 3 

Deviation: 1.5


XEQ X

M?  3 R/S

D?  1.5 R/S


1.70434 x<>y 2.89313 R/S

2.22287 x<>y 2.50113 R/S

1.67644 x<>y 2.87034 ...

(results will vary)




Happy programming,


Eddie 


All original content copyright, © 2011-2022.  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. 


Sunday, May 4, 2014

PCALC Programming: An Introduction

This blog entry is an introduction to programming the PCalc iOS App by James Thomson. My review of this app can be found here:

http://edspi31415.blogspot.com/2014/04/greetings-from-seattle-and-short-review.html

** Note, PCalc Lite app will not have this functionality. To get it, you have to purchase either the full PCalc iOS app (usually sold for $9.99), or make an in-app purchase of the appropriate add-on.

Creating or Edit Programs

Starting New Program or Edit a Program:

1. Press the f(x) button.
2. Select Edit (lower left corner of the dialogue box). This will cause a red circle to appear next to "User" and any other customized categories.
3. Select a category (User will work). To edit an existing program, just select it. To create a new program, select the Plus (+) symbol on the upper right hand corner of the screen. You can delete programs by pressing the red circle with a minus sign next to the program name. A red Delete box will appear as an indicator of confirmation.

Programs in PCalc work with registers. The X register is the primary display register. The Y register is the second display register. PCalc also has 10 permanent memory registers (labeled Memory 0 through Memory 9), 16 temporary registers (labeled Register 0 through Register 9, then Register A through Register F), and a tax register.

Everything is done in a sequential manner.

Types of Commands

There are several types of commands.

Mode Commands: they change the mode of PCalc. This includes angle setting (Degrees Mode, Radians Mode) and base setting (Decimal Mode, Octal Mode, Binary Mode, Hexadecimal Mode).

One Argument Commands: A command operates on a register and stores the result in that register. This includes trigonometric and logarithmic commands. This also includes several number operations:

Negate: Multiply the register by -1. This is like the change sign key. (+/-)

Invert: Takes the reciprocal of the value of the designated register. (1/x)

Truncate: Takes the integer part of the value of the register. (INTG/IP)

Exponent: The exponential function (e^x)

Factorial: The factorial function (x!). x must be positive, but does not have to be an integer.

Two Argument Commands: This is the arithmetic operations, power commands, and the Set command. The format is like this:

Command (operation)
Register (result is stored here)
Value (value of a designed register or specified value)

Let A and B be registers, B can also be a value.

Add B To A: A + B is stored in A

Subtract B From A: A - B is stored in A

Multiply A By B: A × B is stored in A

Divide A By B: A ÷ B is stored in A

A To Power of B: A^B is stored in A

(Inverse Power):
A To Power Of 1/B: Bth root of A stored in A (principal root)

Skipping Functions: PCalc allows for Boolean comparisons of values.

Command: Skip (what has to happen for the next commands to be skipped)
Register (register to be compared)
Value (register or value that is compared)
Skip (number of steps if the comparison is true)

For example:
Command: Skip If Greater Than
Register: X
Value: 42
Skip: 1

Skip 1 step if X>42.

There is a plain Skip command to arbitrarily skip a number of commands. This can turn out to be useful. It may take practice to get the Skip commands correct.

There are no loop commands in PCalc (as of this blog post).

You can stop, even invoke the error condition at any time by inserting a Stop or Error command, respectively.

Let's go over a couple of examples.

Volume of a Sphere

The volume of a sphere is V = 4/3 * π * r^3, where r is the radius.

This program takes the radius in the X register and calculate the volume of the radius. Please pay careful attention to the order of the program steps.

Volume of a Sphere
Decimal Mode
X To The Power of 3
Multiply X By 4/3
Multiply X By Pi (scroll down the possible list of values to select π).


Test example: The radius of Earth is approximately 3,963 miles. If we treat Earth as a sphere, it's volume would approximately be 260,711,882,973.332 cubic miles.

In more complex programs, I start by making copies of X, Y, and any other required registers into temporary registers. I use temporary registers to store and execute immediate calculations. When all the calculations are finished, I store the results into X, Y, and permanent memory registers (if necessary). The next program, Rect > Polar, will be an example of this.

Convert Rectangular Coordinates to Polar Coordinates

Enter the y coordinate then the x coordinate. The result will have r in the X register and the angle in the Y register. The angle is shown in degrees and ranges from -180° to 180°, similar to most scientific calculators with this function.

Comments are followed by a double backwards slash characters ( \\ ). These are for notes only and are not entered.

Rect > Polar
Decimal Mode
Degrees Mode \\ set PCALC to degrees
Set R0 to X \\ start calculating r = √(x^2 + y^2)
R0 To The Power of 2
Set R1 To Y
R1 To The Power of 2
Add R1 To R0
R0 To The Power of 1/2 \\ Inverse Power command, R0 = r
Set R1 To Y \\ start calculation for angle
Skip 5 If X!=0 \\ skip the next 5 steps if x≠0 - Goto (I)
Skip 2 If Y<0
Set R1 To 90
Skip 10 \\ skip the next 10 steps - Goto (II)
Set R1 To -90
Skip 8 \\ Goto (II)
Divide R1 By X \\ Label (I)
Inverse Tangent R1 \\ atan(R1)
Skip 2 If X>=0 \\ block if x<0 and y≥0
Skip 1 If Y<0
Add 180 To R1
Skip 2 If X>=0 \\ block if x<0 and y<0
Skip 1 If Y>=0
Subtract 180 From R1 \\ R1 = angle
Set X To R0 \\ Label (II) - r is now in register X
Set Y To R1 \\ angle is now in register Y, end of program


How to enter (x,y):
RPN Mode On: y, enter key, x, f(x) key, select Rect > Polar
RPN Mode Off: y, x~y key, x, f(x) key, select Rect > Polar

How to View Results:
RPN Mode On: r is displayed on the X stack, angle on the Y stack
RPN Mode Off: r is displayed, press the x~y key to get the angle

Example data (x,y):
y = 3, x = 3: r = 4.242640687, angle = 45
y = -4, x = 3: r = 5, angle = -53.13010235
y = 2, x = -2: r = 2.82842715, angle = 135
y = -3, x = -2: r = 3.60551275, angle = -123.6900675


My next blog entry will have several more example programs using PCalc.

Eddie


This blog is property of Edward Shore. 2014


Python (TI-84 Evo) and HP 32S: Shannon-Wiener Diversity Index

Python (TI-84 Evo) and HP 32S: Shannon-Wiener Diversity Index Introduction The Shannon-Wiener Diversity Index measures a leve...