Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/PhilRunninger/cmp-rpncalc
nvim-cmp source for math calculations using Reverse Polish Notation
https://github.com/PhilRunninger/cmp-rpncalc
neovim-plugin nvim-cmp rpn-calculator
Last synced: 3 months ago
JSON representation
nvim-cmp source for math calculations using Reverse Polish Notation
- Host: GitHub
- URL: https://github.com/PhilRunninger/cmp-rpncalc
- Owner: PhilRunninger
- Created: 2022-02-16T16:45:56.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-01-17T08:37:41.000Z (10 months ago)
- Last Synced: 2024-04-16T02:42:20.767Z (7 months ago)
- Topics: neovim-plugin, nvim-cmp, rpn-calculator
- Language: Lua
- Homepage:
- Size: 96.7 KB
- Stars: 12
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# cmp-rpncalc
An [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) source for math calculations using Reverse Polish Notation## Installation
Use your favorite plugin manager. If you don't have one, try one of these: [vim-pathogen](https://github.com/tpope/vim-pathogen), [vim-plug](https://github.com/junegunn/vim-plug), [Packer.nvim](https://github.com/wbthomason/packer.nvim) or [lazy.nvim](https://github.com/folke/lazy.nvim). Alternatively, you can use packages and submodules, as Greg Hurrell ([@wincent](https://github.com/wincent)) describes in his excellent Youtube video: [Vim screencast #75: Plugin managers](https://www.youtube.com/watch?v=X2_R3uxDN6g)
## Setup
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.
```lua
require'cmp'.setup {
sources = {
{ name = 'rpncalc' } -- Add this to the sources list.
}
}
```## How Does RPN Work?
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=110$ | `73 37 +` |
| $462\div11=42$ | `462 11 /` |
| $\lvert((1+2)\times(3-4))^5\rvert=243$ | `1 2 + 3 4 - * 5 ** abs` |
| $\tan^{-1}(\frac{1}{\sqrt{3}})=30^\circ$| `1 3 sqrt / atan deg`
or
`3 sqrt \ atan deg` |
| If ${a=\frac{\sqrt{7}}{4}}$, $48a^2-\frac{98}{a^4}=-491$ | `7 sqrt 4 / sto 2 ** 48 * 98 rcl 4 ** / -` |
| Euler's Identity: $e^{i\pi}+1=0$ | `e i pi * ** 1 +`
Round-off error gives the answer $\scriptsize{0+1.2246467991474\times{10}^{-16}i}$. |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.
## Complex Numbers
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](https://en.wikipedia.org/wiki/Complex_logarithm)
* [exponentiation](https://en.wikipedia.org/wiki/exponential_function#computation_of_ab_where_both_a_and_b_are_complex)
* [ordinary trig functions](https://en.wikipedia.org/wiki/sine_and_cosine#complex_exponential_function_definitions)
* [inverse trig functions](https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_complex_plane)
* [hyperbolic trig functions](https://en.wikipedia.org/wiki/Hyperbolic_sin#Hyperbolic_functions_for_complex_numbers)
* [inverse hyperbolic trig functions](https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions)## Operands
Operands can take on any of these forms:
* Decimal (base 10): integer `42`, float `-3.14`, or scientific notation `6.02e23`
* Binary (base 2): `0b` prefix, followed by digits `0` and `1`.
* Hexadecimal (base 16): `0x` prefix, followed by digits `0`-`9` or letters `a`-`f` or `A`-`F`.
* Complex: an ordered pair of numbers in any of the prior formats. For example,
`1.2e-4,0x43` equates to `0.00012+67i` in decimal notation.## Operators
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 $\scriptsize0b\normalsize{1100}\text{ AND }\scriptsize0b\normalsize{1010}=\scriptsize0b\normalsize{1000}$ $12\text{ AND }10=8$ | ℕatural |
| \| | OR $\scriptsize0b\normalsize{1100}\text{ OR }\scriptsize0b\normalsize{1010}=\scriptsize0b\normalsize{1110}$ $12\text{ OR }10=14$ | ℕatural |
| ^ | XOR $\scriptsize0b\normalsize{1100}\text{ XOR }\scriptsize0b\normalsize{1010}=\scriptsize0b\normalsize{0110}$ $12\text{ XOR }10=6$ | ℕatural |
| ~ | NOT $\text{NOT }\scriptsize{0b}\normalsize{1010}=-\scriptsize{0b}\normalsize{1011}$ $\text{NOT }10=-11$
All bits are flipped, and a [two's complement conversion](https://en.wikipedia.org/wiki/Two's_complement#Converting_from_two's_complement_representation) of the result is displayed.
$58 = \scriptsize{0b}\normalsize{00111010}$
$\scriptsize{\text{[NOT}\rightarrow\text{] }} = \scriptsize{0b}\normalsize{11000101}$
$\scriptsize{\text{[2's complement}\rightarrow\text{] }}\normalsize{ = -2^7+2^6+2^2+2^0=-128+64+4+1=-59}$ | ℕatural |
| << | Left Shift _(_**Y** *shifted* **X** *bits)* ${\scriptsize0b\normalsize{1}\overleftarrow{11}}^{\text{ }2}=\scriptsize0b\normalsize{11100}$ ${\overleftarrow{7}}^{\text{ }2}=28$ | ℕatural |
| >> | Right Shift _(_**Y** *shifted* **X** *bits)* ${\scriptsize0b\normalsize{110}\overrightarrow{100}}^{\text{ }3}=\scriptsize0b\normalsize{110}$ ${\overrightarrow{52}}^{\text{ }3}=6$ | ℕatural |
| |
**Statistics** | |
| ! | Factorial of **X** $X!={\prod\limits^{X}_{i=1}{i}}$ | ℕatural |
| perm | Permutation of **Y** things taken **X** at a time $_YP_X={\frac{Y!}{(Y-X)!}}$ | ℕatural |
| comb | Combination of **Y** things taken **X** at a time $_YC_X={\frac{Y!}{X!(Y-X)!}}$ | ℕatural |
| n | Sample size *(size of the stack)* | ℂomplex |
| mean | Average of all numbers on the stack. $\bar{x}={\frac{1}{n}}{\sum\limits^{n}_{i=1}{x_i}}$ | ℂomplex |
| sum | Sum of all numbers on the stack ${\sum\limits^{n}_{i=1}{x_i}}$ | ℂomplex |
| ssq | Sum of squares of all numbers on the stack ${\sum\limits^{n}_{i=1}{x_i}^2}$ | ℂomplex |
| std | Sample standard deviation of all numbers on the stack $s={\sqrt{\frac{{\sum\limits^{n}_{i=1}(x_i-\bar{x})^2}}{n-1}}}$ | ℝ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 |
| | $^*$ Non-integer values are truncated. Negatives are formatted as human readable:
$-23=-\scriptsize{0b}\normalsize{10111}=-\scriptsize{0x}\normalsize{17}$
as opposed to
$\scriptsize{0b}\normalsize{1...1111111111111111111111111101001}$ or $\scriptsize{0x}\normalsize{\text{F...FFFFFE9}}$ | ℝeal |
| |
**Constants** | |
| pi | Ratio of a circle's circumference to its diameter $\pi={3.1415926535898...}$ | ℝeal |
| e | Euler's number $e={\sum\limits^{\infty}_{i=0}{\frac{1}{i!}}=2.7182818284590...}$ | ℝeal |
| phi | The golden ratio $\phi={\frac{\sqrt{5}+1}{2}}=1.6180339887499...$ | ℝeal |
| i | The imaginary unit number $i={\sqrt{-1}}$ | ℂ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 |## Disclaimer ⚠
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.## Feedback 📣
This was mainly an exercise to learn lua, and to write a Neovim plugin by porting my prior [Ruby and Erlang rpn calculators](https://github.com/PhilRunninger/rpn). 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.