Command line RPN(Reverse Polish Notation) calculator rpn-cli

RPN(Reverse Polish Notation) calculator for command line.

Similar to unix dc command but you can write formula in command line parameters not only stdin like below.

>rpn 1 2 +
#result:3

>echo $(rpn 1 2 +)
#result:3

dc style is also suported(-s option). You can mix stdin formula and command line.

>rpn -s <<<'1 2 +'
#result:3

>echo '1 2'|rpn -s +
#same as rpn 1 2 +

>rpn '1 1 1 +'|rpn -s +
#pipeline,same as echo '1 2'|rpn -s +

Compare to ruby rcalc, rpn-cli is simple but is designed for easy and short to type formula. For example, "(sin(1/3) - cos(1/3))^2" is "3 inv dup sin swap cos - sqr p" in rcalc, "3ipswc-2^" in rpn-cli.

All operators are single charactor and not necessary to be escaped in bash shell.

Multiply can be "x" (to avoid bash * wildcard)

>rpn 2 2 x
#result:4

Seperator(white space) can be comma(,) or omit it

>rpn 2,2x
#result:4

>rpn 1,1,1,1++++
#result:4

You can use rpn-cli as module.

rpn=require("rpn-cli");
try{
    rs=rpn([1,2,'+']);
    console.log(rs[0]); //'3'
}catch(e){
    console.error(e);
}

Install

sudo npm install -g rpn-cli

Usage

rpn [-h?dsb] <formula>

options:
    -d:debug
    -s:mix stdin "<stdin formula> <command line formula>"
    -b:64 digits precition mode (default:14 digits)
    -h/-?:this help

operators:
    +:add last two stack elements
    -:subtract element 1 from element 2
    x:multiply last two stack elements (* is ok but need escape in bash)
    /:divide element 2 by element 1
    %:element 2 mod element 1

    ^:raise element 2 to the power of element 1
    v:n-th root (element 1 root of element 2)
    2v:(combination) squrare root

    n:Negate last element
    i:Invert last element
    a:Absolute value (last element)
    l:Log e (last element)
    L:exponent of e (last element,reverse function of Log e)
    f:Factorial (last element,! is ok but need escape in bash)

    s:Sin (last element,ragian)
    c:Cos (last element,ragian)
    t:Tan (last element,radian)
    S:arcSin (last element,radian)
    C:arcCos (last element,radian)
    T:arcTan (last element,radian)
    P/180x:(combination) radian -> degree (last element)
    180/Px:(combination) degree -> radian (last element)

    _:floor last element
    =:round last element
    1+_:(combination) ceil last element

    w:sWap last 2 elements
    r:Rotate all elements(1 2 3 -> 3 1 2)
    R:Rotate all elements(1 2 3 -> 2 3 1)
    d:Drop last element
    p:duP last element

constants:
    P:Pi
    1E:(combination) base of natural logarithm(e)

number:
    e:Exponent (5e3 -> 5000/5e-3 -> 0.005)
    -:negative(<any operator/number>- -> subtract,for example:1-1 -> 1 - 1 but 1 -1 -> 1 -1)

example:
    rpn 1 2 +  #result:3
    rpn 1 2 1++ #result:4
    rpn 1,2,1++ #result:4

    rpn 1 2 1 + +|rpn -s 3 + #pipeline:same as rpn 1 2 1 + + 3 +
    rpn -s <<<'1 2 +' #dc style

    rpn 5e3 5 / #same as rpn 5000 5 /
    rpn 10 3 /u  #round:3.3333 -> 3

Change Log

kanasys.com > Economizing Technology > Command line RPN(Reverse Polish Notation) calculator rpn-cli