node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org ยป



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 +

>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


rpn [-h?dsb] <formula>
version -
Copyright(c) 2017-2018,kssfilo(

    -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)

    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 /=  #round:3.3333 -> 3

Change Log

  • 2.0.0:breaking change:store(y/q)/recall(Y/Q) -> store(Y/Q)/recall(y/q)
  • 1.1.0:added statistics operator:count(N)/sum(A)/product(X)/max(M)/min(m)
  • 1.1.0:added register feature:store(y/q)/recall(Y/Q)
  • 1.1.0:added various removing operator:except last element(V)/all(D)
  • 0.3.x:added sin(s)/cos(c)/tan(t)/asin(S)/acos(C)/atan(T)/Pi(P)/v(n-th root)/R(reverse rotate)
  • 0.3.x:s(square root) has been deprecated(use 2v)
  • 0.3.x:c(ceil) has been deprecated(use 1+_)
  • 0.2.x:added factorial(f)/drop(d)/dup(p)/log(l)/exponent(L)/-b option
  • 0.2.x:constant E has been deprecated(use 1E)
  • 0.1.x:added ceil(c)/floor(f)/round(r)
  • 0.0.x:first release