CALC(1) CALC(1) Name calc – RPN calculator SYNOPSIS calc calc < commands DESCRIPTION calc is a RPN calculator in the spirit of the HP calculators. CALC SETTINGS dsp Control the number of stack entries to display (4 by default). 4: 3: 2: 1: 1.00 > 2 dsp 2: 1: 1.00 > prec Specify float precision (2 by default). 1: 3.14 > 4 prec 1: 3.1416 > rad Switch to radius mode. deg Switch to degree mode. STACK MANAGEMENT clear Clear the stack. dropn Remove the n topmost elements of the stack. 4: 2.00 3: 33.00 2: 44.00 1: 55.00 > 3 dropn 4: 3: 2: 1: 2.00 > drop2 Shortcut for 2 dropn. drop Shortcut for 1 dropn. dupn Duplicate the n topmost elements of the stack. 4: 3: 2: 3.14 1: 34.00 > 2 dupn 4: 3.14 3: 34.00 2: 3.14 1: 34.00 > dup2 Shortcut for 2 dupn. dup Shortcut for 1 dupn. pick Pick an element in the stack and copy it at the top of the stack. 4: 3: 123.00 2: 33.00 1: 44.00 > 3 pick 4: 123.00 3: 33.00 2: 44.00 1: 123.00 > over Shortcut for 2 pick. roll Pick an element in the stack and move it at the top of the stack. 4: 3: 123.00 2: 33.00 1: 44.00 > 3 roll 4: 3: 33.00 2: 44.00 1: 123.00 > swap Shortcut for 2 roll. rot Shortcut for 3 roll. rolld Pick the topmost element and move it downward the stack. 4: 1.00 3: 2.00 2: 3.00 1: 4.00 > 3 rolld 4: 1.00 3: 4.00 2: 2.00 1: 3.00 > depth Push the size of the stack on the stack. ARITHMETIC OPERATORS add, + Add two topmost elements of the stack. sub, - Substract two topmost elements of the stack. mul, * Multiply two topmost elements of the stack. div, / Divide two topmost elements of the stack. modulo, % Modulo of two topmost elements of the stack. pow Exponentiation operator of two topmost elements of the stack. sq Shortcut for 2 pow (square value of topmost element). sqrt Shortcut for .5 pow (square root value of topmost element). neg Negate topmost element of the stack. abs Replace topmost element of the stack by its absolute value. ceil Replace topmost element of the stack by its ceiling value. floor Replace topmost element of the stack by its floor value. round Replace topmost element of the stack by its round value. sum Sum every element of the stack. avg Compute average value of the stack. COMPARATORS equal Test if second element equals first one. le Test if second element is less or equal compared to first one. lt Test if second element is less than first one. ge Test if second element is greater or equal compared to first one. gt Test if second element is greater than first one. BASE NUMBERS calc accepts numbers in 'base mode' format, using the following syntax: #...b binary base number (example #01b) #...o octal base number (example #12o) #...d decimal base number (example #23d) #...h hexadecimal base number (example #ea1h) Base numbers are displayed in decimal base by default. See commands bin, oct, dec, hex to change the default base number display mode. base Turn a float number into a base number. float Turn a base number into a float number. bin Display base numbers in base 2. oct Display base numbers in base 8. dec Display base numbers in base 10. hex Display base numbers in base 16. BITWISE OPERATORS blshift Binary left shift operator. 4: 3: 2: 1: 1.00 > 2 blshift 4: 3: 2: 1: 4.00 > brshift Binary right shift operator. band Binary and between two topmost elements. bor Binary or between two topmost elements. bxor Binary xor between two topmost elements. CONSTANTS pi The Pi constant. e Natural logarithm. TRIGONOMETRY cos Cosinus. sin Sinus. tan Tangent. acos Arc-cosinus. asin Arc-sinus. atan Arc-tangent. cosh Hyperbolic-cosinus. sinh Hyperbolic-sinus. tanh Hyperbolic-tangent. LABELS calc supports the notion of labels, where values can be stored to / retrieved from. A valid label name is made of alphabetic characters and underscore character. A label can be entered as is. If a value is already assigned to a label, the stored value is pushed on the stack. Otherwise, the label name is put on the stack. To put a label on the stack without evaluation, enter it in single quotes. mem Dump a list of labels and their associated value. sto Store a value into a label: 4: 3: 2: 1: > 2 'two' sto 4: 3: 2: 1: > mem two: 2.00 4: 3: 2: 1: > two 4: 3: 2: 1: 2.00 > sto+ Increment value stored in a label with the topmost value of the stack. 4: 3: 2: 1: 5.00 > 'two' sto+ 4: 3: 2: 1: > two 4: 3: 2: 1: 7.00 > rcl Recall a value stored in a label. 4: 3: 2: 1: > 'two' rcl 4: 3: 2: 1: 7.00 > purge Unassign value stored in a label. 4: 3: 2: 1: > 'two' purge PROGRAMS <<...>> Sequences of commands can be specified between double angle brackets: 4: 3: 2: 1: > <<3 4 5 6>> 4: 3: 2: 1: <<3.00 4.00 5.00 6.00>> > eval A program on top of the stack can be evaluted with the eval command: 4: 3: 2: 1: <<3.00 4.00 5.00 6.00>> > eval 4: 3.00 3: 4.00 2: 5.00 1: 6.00 > CONTROL FLOW ift If-then conditionnal. Expect a program as a first argument, and an int value as a second argument. program is evaluated if float value is not zero. 4: 3: 2: 1 1: <<3.00 4.00 add>> > ift 4: 3: 2: 1: 7.00 > ifte If-then-else conditionnal. Expect programs on first and second arguments, and an int value as a third argument. Evaluate first program if third argument is not zero, evaluate second program otherwise. 4: 3: 0 2: <<1.00 1.00 add>> 1: <<2.00 2.00 mul>> > ifte 4: 3: 2: 1: 2.00 > whileop While loop operator. Expect programs on first and second arguments. Evaluate first program as long a second program evaluate to a float value that is not zero. COMMENTS The '@' character mark the beginning of a comment. Following characters until EOL are discarded. 4: 3: 2: 1: > @ this is a comment RCFILE On startup, calc parse the $HOME/.config/calc/calcrc if it exists. You can store there your custom commands. For example, this rc file defines a rgb command by storing a program into the 'rgb' label: $ cat ~/.config/calc/calcrc @ 'rgb' command to convert an hexadecimal color value into RGB format <<dup 16 brshift #ffh band swap dup 8 brshift #ffh band swap #ffh band>> 'rgb' sto $ calc 4: 3: 2: 1: > #63c6a5h rgb 4: 3: #99d 2: #198d 1: #165d > AUTHOR Gerome Fournier <jef@foutaise.org>. May 2019