An nvim-cmp source for math calculations using Reverse Polish Notation
Use your favorite plugin manager. If you don't have one, try one of these: vim-pathogen, vim-plug, Packer.nvim or lazy.nvim. Alternatively, you can use packages and submodules, as Greg Hurrell (@wincent) describes in his excellent Youtube video: Vim screencast #75: Plugin managers
There is no setup required specifically for this plugin; however, you need to add rpncalc to the list of sources in your nvim-cmp setup. The following snippet shows how to do that.
require'cmp'.setup {
sources = {
{ name = 'rpncalc' } -- Add this to the sources list.
}
}
RPN is a mathematical notation in which an operator follows its operand(s). This means there is no need for parentheses. Here are some examples, comparing algebraic notation to RPN.
Algebraic | RPN (this plugin's flavor) |
---|---|
73 37 + |
|
462 11 / |
|
1 2 + 3 4 - * 5 ** abs |
|
1 3 sqrt / atan deg or 3 sqrt \ atan deg
|
|
If |
7 sqrt 4 / sto 2 ** 48 * 98 rcl 4 ** / - |
Euler's Identity: |
e i pi * ** 1 + Round-off error gives the answer |
Reading an RPN expression from left to right, numbers are placed on a stack. The top four numbers are labeled X, Y, Z, and T from the top down. These labels are not shown when using the plugin, but they are referenced in the README and the documentation. When an operator is encountered, one or more numbers (as needed by the operator) are popped from the stack, and the result of the operation is pushed back onto the stack.
Most of the operators will work on complex numbers. The following Wikipedia pages were used as reference for some of the more arcane complex numbers calculations. Where the complete answer is an infinte number of values, only the principal value is given.
- logarithms
- exponentiation
- ordinary trig functions
- inverse trig functions
- hyperbolic trig functions
- inverse hyperbolic trig functions
Operands can take on any of these forms:
- Decimal (base 10): integer
42
, float-3.14
, or scientific notation6.02e23
- Binary (base 2):
0b
prefix, followed by digits0
and1
. - Hexadecimal (base 16):
0x
prefix, followed by digits0
-9
or lettersa
-f
orA
-F
. - Complex: an ordered pair of numbers in any of the prior formats. For example,
1.2e-4,0x43
equates to0.00012+67i
in decimal notation.
The Domain column in the following table indicates the types of numbers that are valid for each operator. The possible domains are:
- ℕatural - non-negative integers
- ℝeal - real numbers (includes ℕatural)
- ℂomplex - complex numbers (includes ℝeal and ℕatural)
Operator | Function | Domain |
---|---|---|
Basic Arithmetic |
||
+ | Addition | ℂomplex |
- | Subtraction | ℂomplex |
* | Multiplication | ℂomplex |
/ | Division | ℂomplex |
div | Integer division | ℂomplex |
% | Modulus (not well-defined for negatives) | ℝeal |
abs | Absolute value | ℂomplex |
arg | Argument (the angle between X and the positive real axis) | ℂomplex |
chs | Change Sign (negation) | ℂomplex |
Powers & Logs |
||
** | Raise Y to the X power | ℂomplex |
\ | Reciprocal | ℂomplex |
exp | Raise e to the X power | ℂomplex |
ln | Natural Log of X | ℂomplex |
log | Log (base 10) of X | ℂomplex |
log2 | Log (base 2) of X | ℂomplex |
sqrt | Square Root | ℂomplex |
Trigonometry Variations are: a... for inverse and ...h for hyperbolic |
||
sin | Sine, asin, sinh, asinh | ℂomplex |
cos | Cosine, acos, cosh, acosh | ℂomplex |
tan | Tangent, atan, tanh, atanh | ℂomplex |
csc | Cosecant, acsc, csch, acsch | ℂomplex |
sec | Secant, asec, sech, asech | ℂomplex |
cot | Cotangent, acot, coth, acoth | ℂomplex |
Rounding |
||
floor | Round down to nearest integer | ℂomplex |
ceil | Round up to nearest integer | ℂomplex |
round | Round up or down to nearest integer | ℂomplex |
trunc | Round toward zero to nearest integer | ℂomplex |
Bitwise Non-integer operands will be truncated. |
||
& | AND |
ℕatural |
| | OR |
ℕatural |
^ | XOR |
ℕatural |
~ | NOT All bits are flipped, and a two's complement conversion of the result is displayed. |
ℕatural |
<< | Left Shift (Y shifted X bits) |
ℕatural |
>> | Right Shift (Y shifted X bits) |
ℕatural |
Statistics |
||
! | Factorial of X |
ℕatural |
perm | Permutation of Y things taken X at a time |
ℕatural |
comb | Combination of Y things taken X at a time |
ℕatural |
n | Sample size (size of the stack) | ℂomplex |
mean | Average of all numbers on the stack. |
ℂomplex |
sum | Sum of all numbers on the stack |
ℂomplex |
ssq | Sum of squares of all numbers on the stack |
ℂomplex |
std | Sample standard deviation of all numbers on the stack |
ℝeal |
Miscellaneous |
||
hrs | Convert (Z hours:Y minutes:X seconds) to X hours | ℝeal |
hms | Convert X hours to (Z hours:Y minutes:X seconds) | ℝeal |
dec | Print result in decimal (base 10) | ℝeal |
hex | Print result in hexadecimal (base 16) |
ℝeal |
bin | Print result in binary (base 2) |
ℝeal |
as opposed to |
ℝeal | |
Constants |
||
pi | Ratio of a circle's circumference to its diameter |
ℝeal |
e | Euler's number |
ℝeal |
phi | The golden ratio |
ℝeal |
i | The imaginary unit number |
ℂomplex |
Memory and Stack Manipulation |
||
sto | Store the value of X to memory | ℂomplex |
rcl | Recall the value in memory to the stack | ℂomplex |
m+ | Add X to the value in memory | ℂomplex |
m- | Subtract X from the value in memory | ℂomplex |
xy | Swap X and Y on the stack | ℂomplex |
x | Place the value of X from the last operation back on the stack | ℂomplex |
drop | Remove X from the stack | ℂomplex |
The author of this plugin makes no warranties about the completeness, reliability or accuracy of this calculator. Any action you take upon the results you get from it is strictly at your own risk. The author will not be liable for any losses and/or damages in connection with the use of this calculator.
This was mainly an exercise to learn lua, and to write a Neovim plugin by porting my prior Ruby and Erlang rpn calculators. It's quite possible that computational errors made their way in, despite all efforts to ensure the plugin's accuracy. If you spot any errors, or have suggestions for improvements, new operators, etc., create an issue or a pull request.
Finally, I don't know how useful some of the complex number functions are. It was a fun exercise implementing them, but was it just that, an exercise? Leave a comment if you know of any real-world use (pun intended) for perhaps, the inverse hyperbolic cotangent of a complex number.