HP Forums
(12C) Programming Tricks - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (12C) Programming Tricks (/thread-6985.html)



(12C) Programming Tricks - Eddie W. Shore - 10-06-2016 07:41 PM

http://edspi31415.blogspot.com/2016/10/hp-12c-programming-tricks.html

These tricks can help you cut steps in your programming. Remember that it is still important to test results. Happy programming!

Using Last X

This can be a god-send in making programs more efficient. Unless you are using the Platinum Edition, we only have 99 steps to work with. The LST X function returns the x argument that is last used in a calculation.


LSTx Returns:

One-Argument Functions: √, LN, e^x, FRAC, INTG, n!
The x pre-calculation

Two-Argument Functions (Arithmetic): +, -, *, ÷, y^x
The x value in the arithmetic operation


Number of Inputs


I follow a general rule when it comes to the number of inputs required on an HP 12C program. If the number of inputs is 1 or 2, I would have the user enter inputs on the stack and run the program. If the number of inputs is 3 or more, I have the user pre-store the inputs into registers before pressing [R/S] to run the program. Example: If my program calls for a, b, and c, I have the user store a in register 0, b in register 1, and c in register 2, and then have the user press [R/S].

Branching

You can branch a program to different parts of the program. You designate a register as a choice variable. (Rc = 0 for option 0, 1 for option 1, etc). Examples include setting the variable for computing combinations vs permutations, set for month payments vs yearly payments.

See this link for an example: http://edspi31415.blogspot.com/2016/08/hp-12c-combinationbinomial.html

Program format:

RCL Rc
Test value
-
X=0?
GTO (applicable line number)

(applicable section)
(commands and calculations)
GTO 00 (end the program)

Multiply x by 100


Keystroke: [ENTER], 1, [x<>y], [%T]
Result: The X stack has 100*x, the Y stack has x.

Dividing x by 100

Keystrokes: [ENTER], 1, [ % ]
Results: The x stack has x/100, the Y stack has x.

Quickly entering numbers in the form of 10^n (n is an integer)


Keystrokes: 1, [EEX], n
This is useful when n>2. For example, entering 10000 takes five steps normally. With this sequence, 1 [EEX] 5, you only use 3.

Another Way to Double

Keystrokes: [ENTER], [ + ]
Results: X Stack: 2*x. 1 program step saved.

Absolute Value of x.

Keystrokes: 2, [y^x], [ g ] [ √ ]
Result: X Stack: |x|. It is important to square the number first since the HP 12C’s square root function returns an error on negative numbers.

Signum Function of x.

This is the extension of the absolute value sequence shown previously.

Keystrokes: [ENTER], 2, [y^x], [ g ] ( √ ), [ ÷ ]
Result: X Stack: sgn(x)

Modulus (for two positive values)


Keystrokes: a, [ENTER], b, [ ÷ ], [ g ] (LSTx), [x<>y], [ g ] (FRAC), [ * ]
Result: X stack: a MOD b

Comparing Values

We have a value stored in a register, call it Rc, where c obviously can stand for 0, 1, and so on.
Keystrokes: [RCL], c, [ - ], [ g ] (x=0)

If the test is true (x = Rc), the next step is executed. Otherwise, it is skipped.

Six Digit Approximation of π

Since the HP 12C does not have a π button, we’ll have to enter a 10 digit approximation of π, which will take 11 steps. However, if you want an approximation (given most of the time the HP 12C is used on Fix 2 mode), we can use the approximation π ≈ 355/113, which is accurate to six decimal places.

π ≈ 3.14159265359
355/113 ≈ 3.14159292035

Keystrokes: 355, [ENTER], 113, [ ÷ ]

Calculate (1 + n/100)^x without using the TVM Registers


Keystrokes: 1, [ENTER], n, [ % ], [ + ], x, [y^x]


A very handy collection - striegel - 10-07-2016 12:53 AM

Thanks a lot for this. Some I have used before. It's handy to have this set in one place.


RE: (12C) Programming Tricks - Dieter - 10-07-2016 06:43 PM

Eddie, thank you for this collection of useful tips for the 12C and other calculators.
Let me add a few remarks:

(10-06-2016 07:41 PM)Eddie W. Shore Wrote:  Multiply x by 100

Keystroke: [ENTER], 1, [x<>y], [%T]
Result: The X stack has 100*x, the Y stack has x.

I'd say the Y-register after this procedure is 1, not x.

(10-06-2016 07:41 PM)Eddie W. Shore Wrote:  Quickly entering numbers in the form of 10^n (n is an integer)

Keystrokes: 1, [EEX], n
This is useful when n>2. For example, entering 10000 takes five steps normally. With this sequence, 1 [EEX] 5, you only use 3.

Actually only 2. The "1" is not required, a simple [EEX] 5 does it.

(10-06-2016 07:41 PM)Eddie W. Shore Wrote:  Another Way to Double

Keystrokes: [ENTER], [ + ]
Results: X Stack: 2*x. 1 program step saved.

It depends. In most cases "2 [x]" does it, an initial [ENTER] is not required. So both methods usually take the same two steps. On the other hand the proposed method saves X in LstX which can be helpful.

(10-06-2016 07:41 PM)Eddie W. Shore Wrote:  Absolute Value of x.

Keystrokes: 2, [y^x], [ g ] [ √ ]
Result: X Stack: |x|. It is important to square the number first since the HP 12C’s square root function returns an error on negative numbers.

Instead of using the power function, [ENTER] [x] usually is faster. This applies to most calculators.

(10-06-2016 07:41 PM)Eddie W. Shore Wrote:  Signum Function of x.
This is the extension of the absolute value sequence shown previously.

Keystrokes: [ENTER], 2, [y^x], [ g ] ( √ ), [ ÷ ]
Result: X Stack: sgn(x)

There's one ENTER missing.
[ENTER] [ENTER] [x] [g] [√] [÷]
Or maybe [ENTER] [ENTER] 2 [x^x] [g] [√] [÷]

(10-06-2016 07:41 PM)Eddie W. Shore Wrote:  Modulus (for two positive values)

Keystrokes: a, [ENTER], b, [ ÷ ], [ g ] (LSTx), [x<>y], [ g ] (FRAC), [ * ]
Result: X stack: a MOD b

This can cause roundoff errors. Better use a mod b = a – b * int(a/b):
a [ENTER] [ENTER] b [÷] [g] [LSTx] [x<>y] [g] [INTG] [x] [–]
This requires one more stack register (Z) but it returns exact results.

Dieter