0.0.3 • Published 9 years ago

rpn-calculator v0.0.3

Weekly downloads
1,163
License
MIT
Repository
github
Last release
9 years ago

RPN calculator

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.

Build Status Code Climate Coverage Status

Installation

npm install rpn-calculator

Usage

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

Available operators

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.

operatoroperationexample
+addition1 2 + = 3
-subtraction1 2 - = -1
*multiplication2 3 * = 6
/division7 2 / = 3.5
%modulus12 5 % = 2
powexponentiation7 2 pow = 49
maxmaximum of two1 2 max = 2
minminimum of two1 2 min = 1
&bitwise AND15 9 & = 9
|bitwise OR15 9 | = 15
^bitwise XOR15 9 ^ = 6
<<left shift9 2 << = 36
>>sign-propagating right shift9 2 >> = 2
>>>zero-filled right shift9 2 >>> = 2

Tests

npm install && npm test

License

http://opensource.org/licenses/mit-license.html