rpn-cli - Command Line RPN Calculator

RPN(Reverse Polish Notation) HP stype calculator for command line. using math.js engine.

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

>rpn 1 2 +

>echo $(rpn 1 2 +)

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

>rpn -s <<<'1 2 +'

>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

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

>rpn 2,2x

>rpn 1,1,1,1++++

You can use rpn-cli as module.

    console.log(rs[0]); //'3'


sudo npm install -g rpn-cli


cli.js [-h?dsb] <formula>
version 1.1.0
Copyright(c) 2017-2019 @kssfilo(https://kanasys.com/gtech/)
    -s:mix stdin "<stdin formula> <command line formula>"
    -b:64 digits precition mode (default:14 digits)
    -h/-?:this help

    +: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
    D:Drop all elements
    V:Drop except last elements
    p:duP last element

    Y:move last element to 0th Register
    y:recall from 0th Register
    Q:move element 2 to <element 1>th Register
    q:recall from <element 1>th Register

    N:couNt all elements and push to stack
    M:find Max of all elements and push to stack
    m:find Min of all elements and push to stack
    A:sum of All elements and push to stack
    X:product of all elements and push to stack

    F:apply next operator to all elements(1,2,3,F,1,+ -> 2,3,4)

    NyAY/V:(combination) arithmetic mean(avarage)
    NyXYvV:(combination) geometric mean
    NyAY/F-F2^AY1-/2vV:(combination) standard deviation

    1L:(combination) base of natural logarithm(e)

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

    cli.js 1 2 +  #result:3
    cli.js 1 2 1++ #result:4
    cli.js 1,2,1++ #result:4

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

    cli.js 5e3 5 / #same as cli.js 5000 5 /
    cli.js 10 3 /=  #round:3.3333 -> 3

Change Log

Popular Articles from This Page

Top Page

Economizing Technology > rpn-cli - Command Line RPN Calculator