Reverse Polish notation (RPN) is a mathematical notation in which every operator follows all of its operands, in contrast to Polish notation, which puts the operator in the prefix position. It is also known as postfix notation and is parenthesis-free as long as operator arities are fixed.
npm install rpn-calculator
var calculator = require('rpn-calculator');
// 1 + 2 = 3
calculator('1 2 +'); // 3
// 1 + 2 - 3 = 0
calculator('1 2 + 3 -'); // 0
// (1 + 2) * 3 = 9
calculator('1 2 + 3 *'); // 9
Supports scientific notation:
calculator('1e2 1e-2 +'); // 100.01
All operators replicate the default JavaScript behavior except min
and max
which are limited to 2 arguments to satisfy the default arity of RPN calculator.
operator | operation | example |
---|---|---|
+ |
addition | 1 2 + = 3 |
- |
subtraction | 1 2 - = -1 |
* |
multiplication | 2 3 * = 6 |
/ |
division | 7 2 / = 3.5 |
% |
modulus | 12 5 % = 2 |
pow |
exponentiation | 7 2 pow = 49 |
max |
maximum of two | 1 2 max = 2 |
min |
minimum of two | 1 2 min = 1 |
& |
bitwise AND | 15 9 & = 9 |
` | ` | bitwise OR |
^ |
bitwise XOR | 15 9 ^ = 6 |
<< |
left shift | 9 2 << = 36 |
>> |
sign-propagating right shift | 9 2 >> = 2 |
>>> |
zero-filled right shift | 9 2 >>> = 2 |
npm install && npm test