Home

Awesome

 .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. 
| .--------------. | .--------------. | .--------------. | .--------------. | .--------------. | .--------------. |
| |  ____  ____  | | |     _____    | | |   ______     | | |              | | |    ______    | | |   _______    | |
| | |_   ||   _| | | |    |_   _|   | | |  |_   __ \   | | |              | | |   / ____ `.  | | |  |  _____|   | |
| |   | |__| |   | | |      | |     | | |    | |__) |  | | |    ______    | | |   `'  __) |  | | |  | |____     | |
| |   |  __  |   | | |      | |     | | |    |  ___/   | | |   |______|   | | |   _  |__ '.  | | |  '_.____''.  | |
| |  _| |  | |_  | | |     _| |_    | | |   _| |_      | | |              | | |  | \____) |  | | |  | \____) |  | |
| | |____||____| | | |    |_____|   | | |  |_____|     | | |              | | |   \______.'  | | |   \______.'  | |
| |              | | |              | | |              | | |              | | |              | | |              | |
| '--------------' | '--------------' | '--------------' | '--------------' | '--------------' | '--------------' |
 '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'

<sub>(ASCII art by patorjk.com)</sub>

License: MIT C/C++ CI build & test

1. Introduction

What's this project?

HIP-35 is a calculator for hipsters. It emulates the RPN (reverse Polish notation) mode of HP-35 from 1972. It runs a terminal UI built with ncurses that displays the current result on a display.

⚫ Why RPN?

Because it genally involves less keystrokes and it's faster than algebraic notation calculations.

How? RPN makes it very easy to compute long expressions containing lots of brackets [1]. When using it, you don't need to think how terms are grouped anymore.

⚫ The memory

HP-35 had a stack-based memory of 4 registers X, Y, Z, T with X being the bottom (entry). That stack shifts (lifts or drops) automatically as calculations are performed. There are also 10 general registers labelled A to J that store values, acting as buffers.

HP-35HIP-35
<img src="https://raw.githubusercontent.com/leonmavr/HIP35/master/assets/wiki_image.jpg" alt="HP 35 front face" height="325"/><img src="https://raw.githubusercontent.com/leonmavr/HIP35/master/assets/demo_screenshot.png" alt="hip 35 screenshot" height="325"/>

2. Usage

⚫ Dependencies

CMake is required to build the project and ncurses for the frontend. Also you need to make sure your compuler supports C++17 or higher. Install both packages by:

Arch-basedDebian-based
sudo pacman -S cmake ncursessudo apt install cmake libncurses5 libncursesw5

⚫ Compilation

The build system is based on CMake. To build the project run:

cmake . -Bbuild
cd build
make -j

or alternatively just execute the build.sh script:

./build.sh

The demo executable that runs the UI will be generated at

./build/demo/demo

That's it, have fun doing RPN calculations!

A unit test executable is also generated at ./build/test/testhip35.

⚫ The keys

Most keys are self-explanatory. However, some are less straightforward.

keydescriptionsyntaxbeforeargumentafter
CHSchange sign of XCHS4-4
ENTERduplicate X to YENTERT,Z,Y,X=1,2,3,4T,Z,Y,X=2,3,4,4
LASTXlast X before operationLASTX4 E 20 + (X=24)X=20
RDNrotate stack downRDNT,Z,Y,X=1,2,3,4T,Z,Y,X=4,3,2,1
STOstore to A-J e.g. A = XSTO a-jX=3.14, A=0aX=3.14, A=3.14
RCLrecall to X e.g. X = ARCL a-jX=0, F=2.71fX=2.71, F=2.71
EEXexponentiation of XEEX N737000
CLXclear register XCLX123450
CLRclear entire stackCLRT,Z,Y,X=1,2,3,4T,Z,Y,X=0,0,0,0
qquit applicationq

Enter (<space>) needs to be pressed to separate two successive numbers. When running the UI, press q to quit. <Ctr-C> is not captured so q is the only way to quit. You can read more at the HP35 manual [4].

⚫ Project directory structure and source code

Implementations are found at lib/src and header files at lib/inc. The demo application is at demo/main.cpp and unit tests are found at test/test.cpp.

You can create an instance of the Hip35 class and run it either via the UI or without it (given a string) as follows:

#include "hip35.h"
//...
auto hp = std::make_unique<Ui::Hip35>(key::keypad);
// using the UI
hp->RunUI();
// or without it
auto result = hp->EvalString("430 ENTER 80 - 1.2 *");

3. Demo

Second order equation by using storage/recall:

https://github.com/leonmavr/HIP35/assets/13514096/911e7cce-2db0-4705-92fd-c3dfa6142de6

The fraction sqrt(2 + (sin(25) + pi/2)^2)/3:

https://github.com/leonmavr/HIP35/assets/13514096/ee05b992-8901-4ffe-9649-71dfb9cda71c

4. Basic theory

⚫ What is RPN?

RPN allows a stack-based evaluation of numerical expressions, eliminating the need of brackets. There are two main ideas behind RPN:

  1. operands (numbers) are pushed in a stack
  2. it follows the postfix notation, i.e. each operator follows one or two operands preceeding it

Physical calculators need a way to separate successive numbers, accomplished with the enter (E) key.

For example:

+------------+---------------+-------------------+-----------------------------------------------+
| Algebrainc | RPN           | Physical          | Stack contents                                |
| expression | equivalent    | calculator        |                                               |
+------------+---------------+-------------------+-----------------------------------------------+
| 1 + 2      | 1 2 +         | 1 E 2 +           | [1], [1,1], [1,2], [3]                        |
+------------+---------------+-------------------+-----------------------------------------------+
| 1 + 2 - 3  | 1 2 3 + - or: | 1 E 2 E 3 + -     | ...[1,2], [1,2,2], [1,2,3], [1,-1], [0]       |
|            | 1 2 + 3 -     | 1 E 2 + 3 -       | ...[1,2], [3], [3,-3], [0]                    |
+------------+---------------+-------------------+-----------------------------------------------+
| (1+2)(3+4) | 1 2 + 3 4 + * | 1 E 2 + 3 E 4 + * | ...[1,2], [3], [3,3], [3,3,4], [3,7], [21]    |
+------------+---------------+-------------------+-----------------------------------------------+
| 13*sin(37) | 13 37 SIN *   | 13 E 37 SIN *     | [13], [13,13], [13,37] [13,s(37)], [13*s(37)] |
+------------+---------------+-------------------+-----------------------------------------------+

You can practise more with RPN by using the script docs/rpn.py which evaluates RPN expressions from a string. Numbers are separated by space in this script so you don't need to press enter.

⚫ HIP-35 additional features

HIP-35 includes some minor additions compared to HP-35:

You can read more at the wiki page, at HP museum or at the HP-35s manual, which is its successor.

⚫ Cool trivia

5. Future ideas

References

[1] <a name="ref-1"></a>Dr Marshall Leach Jr's page
[2] <a name="ref-2">Interview with David Cochran
[3] <a name="ref-3"></a>History of Space Shuttle Rendezvous, Oct 2011
[4] <a name="ref-4"></a>HP35 manual