Calculator
I often use a mix of hexadecimal and decimal in my calculations, and needed a calculator
that could do this conveniently. I designed this calculator with the following goals:
- Easily switch between decimal and hexadecimal
- Keyboard input for most common operations
This is the documentation page. The calculator is here.
Features
- RPN and infix notation input modes
- Three different button layouts
- Numpad: Button locations match the numeric keypad and navigation keys on a full-size keyboard.
Convenient for keyboard input.
- Hexadecimal: Has buttons for all 16 hexadecimal digits. More convenient for mouse or touchscreen input
where exact correspondance with a keyboard layout is not necessary.
- Touch: Experimental layout for touchscreens. Requires less pointing accuracy by
encoding each digit as a line segment (4 start points × 4 end points = 16 digits).
- Easily switch/convert between bases, even within the same calculation.
- Three dedicated buttons choose between decimal, hexadecimal, and binary bases.
- Holding Shift (decimal) or Ctrl (hexadecimal) when entering the number chooses the base for that number.
- The base can be changed while entering a number, allowing each number to be entered in a different base.
- Touchscreen/mouse optimization: Dragging up or down on a button selects the Shift or Ctrl
variant of the operation without needing to click/tap the Shift or Ctrl button.
- Truncation of a number down to a fixed-sized integer. Useful for emulating two's complement arithmetic on fixed-length binary integers.
Implementation
- Uses the GNU MPFR library for binary floating-point arithmetic.
- Internal calculations use 512 bits of mantissa.
Help
Keyboard input
The top two rows of buttons map to F1 to F12 of the keyboard, with Shift for the top row, and without Shift for the second row.
When a button has a red label above, or blue label below the main label, the red function is accessed by holding
Shift, while the blue function is accessed by holding Ctrl.
Scientific notation
Unlike most calculators, the decimal point and exponent functions share one key. When entering a number, press
∙/EXP once to enter the fractional part, and press
∙/EXP again to enter the exponent. Some examples:
- 1.23: 1∙/EXP23
- 1.23e4: 1∙/EXP23∙/EXP4
- 1e4: 1∙/EXP∙/EXP4
- 0.2: ∙/EXP2
- 0.2e-3: ∙/EXP2∙/EXP+/-3
Backspace key
The backspace key is used for three similar operations:
- Backspace: When entering a number, this deletes the last entered digit. When not
entering a number, this deletes one number or operation in infix mode, or the bottom-most stack entry in RPN mode.
- Clr Stk: Clear stack. Deletes all four stack entries shown in the display.
- CE: Clear Entry. When entering a number, this deletes all of the digits of the number currently being entered. This is
faster than pressing Backspace multiple times, and also won't delete beyond the current number. When not entering a number, CE
does not delete anything, so it won't delete a long expression in infix mode.
Decimal, Hexadecimal, and Binary
Converting between bases is done by changing the display mode using the Bin, Dec, and Hex buttons.
Changing the mode changes the base for all numbers in the display.
Entering numbers: The base can be changed while entering a number, to allow mixed-base expressions.
When entering a number, pressing Bin/Dec/Hex once changes the base only for the number being entered without
changing the display mode. Pressing a second time changes the display mode. The base of a number being entered
can also be changed with the Shift and Ctrl keys. Typing digits while holding Shift changes the base to decimal,
while holding Ctrl changes the base to Hexadecimal.
For example, to calculate C00 (hexadecimal) + 128 (decimal), type
Ctrl+(C00)+Shift+(128)=,
holding Ctrl while typing C00 and holding Shift while typing 128.
The result will be c00+80=c80
in hexadecimal mode or 3072+128=3200
in decimal mode. Individual numbers
being entered can be in a different base than the current display mode.
The same calculation in RPN mode would be
Ctrl+(C00)EnterShift+(128)+,
with a result of c80
(hexadecimal), 3200
(decimal), or 110010000000
(binary).
Typing a digit that exceeds the maximum allowed by the current mode also automatically
changes the base. For example, typing 5 while in binary mode automatically changes the number to decimal, while typing
A changes the number to hexadecimal, regardless of whether Shift or Ctrl are held.
What's the current base?
The current display mode is shown by the yellow highlight on the Bin, Dec, and Hex buttons. This indicates the
base that is used to display numbers, and is also the default base used when entering a new number.
When entering a number, the base of the current number is shown in the display and on the Bin/Dec/Hex buttons.
The right side of the bottom row of the display shows BIN
, DEC
, or HEX
, and
one of the Bin/Dec/Hex button labels has an underline. These are only shown when entering a number. If the yellow highlight is on
a different Bin/Dec/Hex button than the underline, it indicates that the number is being entered in a different base
than the base used to display numbers.
Interesting Calculations
This is the hypot (hypotenuse) function. It computes sqrt(x2 + y2). For example, 34 = 5.
This function computes 1/(1/x + 1/y). This is sometimes called the "parallel" operator, because it can be used to calculate the
resistance of two parallel resistors.
fracint
These two functions take the fractional part and integer part of a number, respectively. For example, for the number 123.45,
the fractional part is 0.45 and the integer part is 123.
mod
This button is located at Shift-÷. It computes the remainder from dividing the first
operand by the second operand. The quotient of the division is rounded towards zero. This function is implemented using mpfr_fmod.
→fp32→fp64←fp32←fp64
These buttons convert raw IEEE 754 floating-point numbers (as 32-bit or 64-bit integers) to and from floating-point
values. For example, 1 →fp32 becomes 0x3f800000 (or 1065353216), and ←fp32
performs the reverse operation to convert 0x3f800000 back to 1.0.
Options
Input notation
There are two input notations supported:
- RPN: Reverse Polish Notation. Many Hewlett-Packard calculators use RPN.
Operations are entered after their operands. For example, to compute 1 + 2 × 3, enter
1Enter2Enter3×+.
- Infix: Most other calculators use infix notation. Operations are entered in between their operands. For example, to compute 1 + 2 × 3, enter
1+2×3=
Unary operations (operations with one operand) are
usually input after their operand (e.g., 4),
but most can also be entered before the operand (e.g., 4)
with the interpretation determined by context. This context-dependent interpretation can be confusing, especially when it interacts
with operator precedence rules. For example, 1x2
results in sqrt(12), but ln1x2
results in (ln(1))2.
The button layout is slightly different between the two modes. RPN mode uses an Enter key
to enter multiple numbers without an operator in between, while infix mode requires (,
), and = keys.
Button layout
The Numpad layout matches the numeric keypad and navigation keys of a typical full-size keyboard.
Hexadecimal numbers can be entered using the numeric keypad while holding down Ctrl. This places the A to F
keys near the numeric keys, to allow entering hexadecimal numbers without jumping all over the keyboard.
The Hexadecimal layout is intended for mouse or touchscreen devices where there is no need for buttons
to be arranged to match a keyboard. With more buttons, all sixteen hexadecimal digits are arranged in numerical
order and available without using Ctrl.
The Touch 42 layout arranges the sixteen hexademical digits into four groups of four. Each digit is entered
by dragging from one of the four start squares to one of four end squares, which provides 16 different combinations.
For example, dragging from the lower-left square to the upper-right square results in the digit 2.
The numbers 0, 4, 8, and C only require a single tap/click because they have the same start and end square.
Because there are only four buttons instead of sixteen, less precise clicking/tapping is required.
In exchange, most digits are line segments instead of just a point.
Display notation
- Normal: Display all digits of a number. Switch to scientific notation when the number gets too long
(integer part of a non-integer exceeding 7 digits, or an integer exceeding the Display options Digits setting)
- Fixed: Always display the same number of digits after the decimal point, adding trailing zeros if necessary.
- Eng.: Engineering notation is scientific notation where the exponent is a multiple of three. The
corresponding SI prefix abbreviation is also displayed in gray. For example, 16384 is displayed as 16.384×103.
- Eng. Binary: This is binary scientific notation where the exponent part is a power of 210.
The corresponding IEC binary prefix abbreviation is also displayed in gray.
For example, 16384 is displayed as 16×210.
- Scientific: Standard scientific notation. The exponent base depends on the selected base. Decimal mode
uses powers of 10. Binary uses powers of 2. Hexadecimal uses powers of 16 (displayed as 2 to a power of a multiple of 4).
For example, 16384 is displayed as 1.6384×104 in decimal, 4×212 in hexadecimal (16384 = 0x4000),
and 1×214 in binary.
Display options
- Digits: Chooses the number of digits after the decimal point to display in Fixed notation. Chooses the
number of significant digits (including the integer part) in Normal, Eng., Eng. Binary, and Scientific notations.
- Group digits: Enables background shading to make long numbers easier to read. In decimal mode, there are three
digits in each group. In binary or hexademical mode, there are four digits per group.
Integer truncate
If set to something other than None, the result of every operation is truncated to an integer of the
selected number of bits. This is useful when you want to use fixed-length two's complement integers
including its overflow behaviour.
Angles
Select degrees or radians as the unit for angles for trigonometric functions.