Port of schuchert.wikispaces.com


Rpn_Calculator_High_Level_Description

Rpn_Calculator_High_Level_Description

Introduction

I got the idea for using a calculator for a TDD problem from a few different people. The first time I tried it, I decided to create an Rpn calculator rather than an infix calculator because I’m a geek and I prefer them. I rediscovered that Rpn calculators are generally easier to create. HP demonstrated this in 1967 with a $5000, 4-function, 3-number calculator that weighed just under 50 pounds, which was an amazing accomplishment.

Background

An Rpn Calculator allows you to calculate results without having to use parenthesis. Here is an image of an HP 10 calculator (from http://www.hpmuseum.org): {align=”center”} A few more links:

Note that for this problem I generally do not fully simulate the calculator. There is one significant difference, which I’ll leave as an exercise for you to discover.

The Problem

Basic calculations on an Rpn Calculator are different from “normal” calculators. What follows are some examples of interactions with the calculator to demonstrate that. Note:

Basic Arithmetic

Basic arithmetic generally involves entering one or more operands and then hitting an operator. The operands come before the operator. Consider each row in the table a separate example, each staring with a fresh calculator:

Calculation Result
342 <enter> 5 * > 1710
4 * > 0
5.3 <enter> 2 - > 3.3
5 <n!> > 120

Notice that earlier operands are “to the left” while later operands are “to the right”. E.g., 5.3 <enter> 2 - resulted in 3.3, not -3.3.

Stack Stuff

A basic HP calculator has 4 registers: x, y, z, t – the x register is also known as the accumulator. The <enter> key pushes things on the stack. What follows is an example of what the stack looks like in a little more detail. Note that for our purposes, we can have any number of values rather than just 4.

In this example, each row is a continuation of the previous rows. This is one big example:

Value Entered X Y Z T
> 45 > 45 > 0 > 0 > 0
<enter> > 45 > 45 > 0 > 0
> 1 > 1 > 45 > 0 > 0
<enter> > 1 > 1 > 45 > 0
> 56 > 56 > 1 > 45 > 0
<enter> > 56 > 56 > 1 > 45
> 66 > 66 > 56 > 1 > 45

Entry Mode

The calculator has three “modes” of data entry:

Here is another example. And as with the previous example, each row builds on the previous rows:

Entry X Y Z T
> 123 > 123 > 0 > 0 > 0
<enter> > 123 > 123 > 0 > 0
> 43 > 43 > 123 > 0 > 0
+ 166 > 0 > 0 > 0
> 321 > 321 > 166 > 0 > 0
<enter> > 321 > 321 > 166 > 0
<enter> > 321 > 321 > 321 > 166
+ > 642 > 321 > 166 > 0
- > -321 > 166 > 0 > 0
+ > -155 > 0 > 0 > 0
- > 155 > 0 > 0 > 0
- > -155 > 0 > 0 > 0

Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.