2 releases
| 0.1.1 | Feb 13, 2022 |
|---|---|
| 0.1.0 | Feb 13, 2022 |
#1950 in Math
22KB
498 lines
RPN calculator
A basic command-line RPN calculator. It's similar to DC but with a few differences.
Numbers are stored as f64 rather than arbitrary-precision integers. This is simpler but sometimes less precise.
Command List
v: Prints the value at the top of the stack without popping it.V: Prints the whole stack including indices. Index0represents the top of the stack.p: Same asvbut pops the value from the stack before printing it.+,-,*,/: Exactly what you expect. The first operand is popped first.%: Popsa, then popsb, then pushesa % b.": A combination of/and%. Same argument order as%, but pushes the quotient before pushing the remainder aka modulus.^: Popsa, then popsb, then pushesa ^ b.~: Popsa, then pushes-a. In other words, negates the value at the top of the stack.c: Clears the stack.d: Popsa, then pushesatwice.r: Swaps the two values at the top of the stack.R: PopsN, then rotates the bottomabs(N)values of the stack. The sign ofNdetermines the rotation direction: negative is right-rotation, positive is left-rotation. The best way to understand this may be to try it yourself.i: Popsa, then sets the input radix toa. The input radix determines how numbers are parsed. In radices less than or equal to 10, all numbers can be input as normal. In radices greater than 10, the number must start with a digit (0 to 9), but you can prefix the number with0and it will result in the same value. For example, in base 16, trying to inputc2as a number would run theccommand, then input the number2, so instead you should input0c2.I: Pushes the input radix.(space),\t(tab),\0(ASCII NUL),\n(newline): Does nothing, but splits numbers and other operations, allowing for multiple on the same line in the case of space, tab, and NUL (e.g.,123 456pushes123and456separately).z: Pushes the current length of the stack, not including this value that is about to be pushed.?<command>: Gets help for a command.
Register Commands
Registers are indexed by their UTF-8 codepoint, which can be any single character. They are their own fully-fledged, freestanding stacks.
s<reg>: Pops a value from the global stack and pushes it to the specified register.l<reg>: Pops a value from the specified register and pushes it to the global stack.S<reg>: Peeks a value from the stack without popping it and pushes it to the specified register.L<reg>: Peeks a value from the specified register and pushes it to the global stack.&<reg><operation>: Performsoperationinside the specified register. Nested register operations such as&asbare not permitted.
Usage Examples
> 2 2+v
4
> c 2~V
0: -2
> 16i c2 V
0: 2
> c 0c2 V
0: 194
> 0a i c 123 sl V
> &lV
0: 123
> 456 sl &l+ Ll v
579
> c 1 2 3 4 V
3: 1
2: 2
1: 3
0: 4
> 3RV
3: 1
2: 3
1: 4
0: 2
> 3~RV
3: 1
2: 2
1: 3
0: 4
> c 0 0 0 0 zp
4
> c 1 0 / p
inf
Dependencies
~235KB