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 into the associated README.md file.

This version also 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.