Enter RPN

Hypotenuse Program for HP-32S
Login

Hypotenuse Program for HP-32S

Motivation

This came up in the context of my PCalc layout. I expected a quick web search to turn one up, but no, it’s apparently too trivial.

Perfect for a rank newbie to HP programming like me, then. 🤓

Calculating the Hypotenuse

Because it is the fashion, I jumped straight to feeding the problem to a vibe-coding app, with this prompt:

Write an HP-32S program for computing the hypotenuse of a right triangle from the x and y stack variables as the other sides. Bonus points for putting it in DM32 state file form.

After adding that I wanted the program labeled H, not A, it gave me this:

# DM32 State File - Hypotenuse Calculator
# HP-32S Program: Computes hypotenuse from X and Y stack variables
# Usage: Enter first side, ENTER, enter second side, XEQ H

# Program in HP-32S format:
# LBL H    - Label the program as 'H'
# x²       - Square X register (first side)
# x⇋y      - Swap X and Y registers  
# x²       - Square new X register (second side)
# +        - Add the two squares
# √x       - Take square root (hypotenuse)
# RTN      - Return

# DM32 State File Format:
# The following uses DM32 encoding where:
# q = x² (square function)
# l = √x (square root function)

[Program H]
LBL H
q
x⇋y
q
+
l
RTN

# Usage Instructions:
# 1. Enter first side length
# 2. Press ENTER
# 3. Enter second side length  
# 4. Press XEQ H
# 5. Result (hypotenuse) appears in X register

# Example:
# For a 3-4-5 right triangle:
# 3 ENTER 4 XEQ H → displays 5

# Mathematical Formula: c = √(a² + b²)
# where c is hypotenuse, a and b are the other two sides

Pretty much exactly what I would have written, albeit with less verbose commentary.

And you know what? It works!

You may download a cleaned-up version and get usage documentation here.

HP-11C/15C Version

Contrast the HP-15C version here, with even less verbosity than my cleaned-up DM32 version.

The conversion required that I rename the program from H to A to fit within the limited set of alphabetic labels.

HP-12C/12c Platinum Version

For an extra challenge, I then translated it to the form a 12c Platinum needs. Note that we lose named labels, and in the case of the 12C, we have to emulate the function in terms of .

Calculating the Leg

We can rearrange this world-famous equation to solve for one of the legs given the other and the hypotenuse as a² = c² - b², which we may then program thus:

L01 LBL L    ; find leg, starting with «T Z c b» from a² = c² - b²
L02 x²       ; T Z c b²
L03 x⇋y      ; T Z b² c
L04 x²       ; T Z b² c²
L05 x⇋y      ; T Z c² b²
L06 -        ; T T Z c²-b²
L07 √x       ; T T Z a
L08 RTN

This is presented as you see it on the DM32’s enhanced displays, plus comments to document stack register movements. The original HP calculators use purely numeric output, so what we show here as line L04 appears on a 15C less helpfully as “004- 43 11”.

Usage: Put in the hypotenuse, hit ENTER, put in the known leg’s length, and then without hitting ENTER a second time, say GSB L.

If you are willing to swap the arguments, you can drop step L05, as that allows the subtraction to proceed without needing a sign change afterward. Personally, I think it’s easier to remember the parameter order by virtuously keeping the legs together, as it were, in x.

Calculating Both: Generalized Right Triangle Solver

Because the core calculation differs by a single sign, it requires but a small amount of cleverness to build a program that takes any two sides of a right triangle, then calculates the missing value. Since we wish to use named storage locations matching those of the variables in the equations above, we need at least an HP-32S for this one:

T01 LBL T   ; find missing value STOred in A² + B² = C² or A² = C² - B²
    RCL A
    RCL B
    RCL C
    x≠0?    ; hypotenuse given?
    GTO L   ; yes; find the leg
    +       ; DROP zero hypotenuse

H01 LBL H   ; find that missing hypotenuse
    x²      ; T Z y x²
    x⇋y     ; T Z x² y
C01 LBL C   ; lets L skip first square operation
    x²      ; T Z x² y²
    +       ; T T Z x²+y²
    √x      ; T T Z answer
    RTN

L01 LBL L   ; rearrange stack to calculate the missing leg's length
    R↓      ; send nonzero hypotenuse up to T
    +       ; correct call requires A or B to be 0 when C is nonzero
    x²
    +/-     ; key calculation difference amounts to "add the negative"
    R↑      ; bring hypotenuse back down into x
    GTO C   ; hijack "find the hypotenuse" method as "find the leg"

The calling convention is simple, if strict: give two of A, B, or C to calculate the third. If C is 0, it uses the straight Pythagorean theorem to calculate the hypotenuse from the length of the legs. Otherwise, it assumes one of the two leg length values is missing and calculates the other under the assumption that A+B means “whichever of A or B happened to be given.”

I left off the line numbers after the LBL points because:

  1. Line numbers obscure the jump points. I want those labels to stand out.

  2. For short routines like this, I can count offsets easily enough, if necessary.

  3. In this specific program, LBL C is not a primary jump point. It exists merely to allow L to jump into the middle of H. If you happen to have an HP 35s there, it has a new feature that lets you drop that label and change the last line to GTO H003 instead, which better expresses the operation of this program.

You might notice that this formulation is a pure extension of the H routine above. You can still call it by putting the legs on the stack, then XEQ H.

You can say XEQ L to mimic our find the leg program above provided you ensure that stack register y or z is 0. This interface is not meant to be awkward; this is merely what our T entrypoint does, so if you wish to harness its internal helper routine like that, you have to call it like T does.

License

This work is © 2025-2026 by Warren Young and is licensed under CC BY-NC-SA 4.0