PhilRunninger / cmp-rpncalc

nvim-cmp source for math calculations using Reverse Polish Notation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cmp-rpncalc

nvim-cmp source for math calculations using Reverse Polish Notation

Installation

Use your favorite plugin manager to install this plugin. vim-pathogen, vim-plug, and Packer.nvim are some of the more popular ones. A lengthy discussion of these and other managers can be found on vi.stackexchange.com.

If you have no favorite, or want to manage your plugins without 3rd-party dependencies, use packages, as described in Greg Hurrell's excellent Youtube video: Vim screencast #75: Plugin managers

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.

require'cmp'.setup {
  sources = {
    { name = 'rpncalc' }
  }
}

How Does RPN Work?

RPN is a mathematical notation in which the 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) Result
$956 + 37$ 956 37 + 993
$\frac{452}{12}$ 452 12 / 37.666666666667
$\tan^{-1}(\frac{1}{\sqrt{3}})$ 1 3 sqrt / atan deg or 3 sqrt \ atan deg 30
$3x^2+6x-5 \vert _{x=\frac{\sqrt{17}}{4}}$ 17 sqrt 4 / xm drop 3 rm 2 ** * 6 rm * + 5 - 4.3721584384265
$e^{i\pi}$ e i pi * ** -1+1.2246467991474e-16i
round off error πŸ™

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. Although these labels are not shown when using the plugin, 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 on the stack.

Complex Numbers

Most of the operators also will process complex numbers. The following Wikipedia pages were used as reference for some of the more arcane derivations for complex numbers. Where the complete answer is an infinte number of answers, only the principal value is given.

Operands

Operands can take on any of these forms:

  • Decimal - integer, floating point, scientific notation
  • Binary - 0b prefix, followed by digits 0 and 1.
  • Hexadecimal - 0x prefix, followed by digits 0-9 and letters a-f.
  • Complex - an ordered pair of numbers in any of the above formats. For example 1.2,-0x43 represents 1.2-67i.

Operators

The operator categories and certain exceptional functions show the types of numbers for which they are valid.

  • ℝ - real
  • β„• - natural
  • β„‚ - complex

Basic Arithmetic - ℝ and β„‚

  • + - Addition
  • - - Subtraction
  • * - Multiplication
  • / - Division
  • div - Integer division
  • % - Modulus (ℝ only)
  • abs - Absolute value
  • arg - The angle between X and the x-axis
  • chs - Negation

Powers & Logs - ℝ and β„‚

  • exp - Raise e to the X power
  • log - Natural Log of X
  • log10 - Log (base 10) of X
  • log2 - Log (base 2) of X
  • sqrt - Square Root
  • ** - Raise Y to the X power
  • \ - Reciprocal

Trigonometry - ℝ and β„‚

Variations: a*** are inverse, and ***h are hyperbolic.

  • Sine - sin asin sinh asinh
  • Cosine - cos acos cosh acosh
  • Tangent - tan atan tanh atanh
  • Cosecant - csc acsc csch acsch
  • Secant - sec asec sech asech
  • Cotangent - cot acot coth acoth

Rounding - ℝ and β„‚

  • floor - Round down to nearest integer
  • ceil - Round up to nearest integer
  • round - Round to nearest integer
  • trunc - Truncate to integer

Bitwise - β„•

These operators will truncate non-integer operands.

  • & - AND
  • | - OR
  • ^ - XOR
  • ~ - NOT
  • << - Left Shift (Y shifted X places)
  • >> - Right Shift (Y shifted X places)

Constants - ℝ and β„‚

  • pi - 3.141592653...
  • e - 2.718281828...
  • phi - the golden ratio, 1.618033989...
  • i - 0+1i

Statistics - ℝ

  • ! - Factorial
  • perm - Permutation of Y things taken X at a time
  • comb - Combination of Y things taken X at a time
  • mean - Average of all numbers on the stack (also β„‚)
  • std - Sample standard deviation of all numbers on the stack
  • sum - Sum of all numbers on the stack (also β„‚)
  • ssq - Sum of squares of all numbers on the stack (also β„‚)
  • count - Sample size

Memory and Stack Manipulation - ℝ and β„‚

  • xm - Store the value of X to memory
  • rm - Recall the value in memory and put it on the stack
  • m+ - Add X to the value in memory
  • m- - Subtract X from the value in memory
  • xy - Swap X and Y on the stack
  • x - Place the value of X from the last operation back on the stack
  • drop - Remove X from the stack

Miscellaneous - ℝ

  • hrs - Convert (Z hours:Y minutes:X seconds) to X hours
  • hms - Convert X hours to (Z hours:Y minutes:X seconds)
  • bin - Print results in binary (base 2).
  • hex - Print results in hexadecimal (base 16).
  • dec - Print results in decimal (base 10).

Disclaimer ⚠

The author of this plugin does not make any 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 Neovim plugins 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 its 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 (in an issue is fine) if you know of any real-world use (pun intended) for, let's say, the inverse hyperbolic cotangent of a complex number.

About

nvim-cmp source for math calculations using Reverse Polish Notation


Languages

Language:Lua 100.0%