dc tutorial =========== Table of Contents ================= 1 Introduction 2 Simple calculation 3 Scale And Base 4 Stack 5 Registers 6 Macros 7 Conditionals and Loops 8 Next 1 Introduction ~~~~~~~~~~~~~~ dc(1) is a non standard, but commonly found, reverse-polish Desk Calculator. According to Ken Thompson, "dc is the oldest language on Unix; it was written on the PDP-7 and ported to the PDP-11 before Unix [itself] was ported". Historically the standard bc(1) has been implemented as a front-end to dc. 2 Simple calculation ~~~~~~~~~~~~~~~~~~~~ In brief, the reverse polish notation means that you first put the numbers (on a stack) and then apply an operation on these. Instead of writing 1+1, you write 1 1+. By default dc, unlike bc, doesn't print anything, the result is just pushed on the stack. You have to use to use the command "p" to print the element at the top of the stack. dc. Thus a simple operation looks like: $ dc <<< '1 1+pq' 2 This example uses a "here string" present in bash 3.x, ksh93 and zsh. if your shell doesn't support this you can use echo '1 1+p' | dc or if you have gnu dc, you can use dc -e '1 1 +p'. Of course, you can also just run "dc" and enter the commands. The classic operations are present + - / * % ^ and v, respectively addition, substracton, multiplication, division, remainder, exponentiation and square root. (gnu dc adds a couple more). To input a negative number you need to use the _ underscore character. $ dc <<< '1_1-p' 2 You can use the digits 0 to 9 and the letters A to F for the numbers as well as a dot (.) to separate the decimals. The A to F **must** be capital letters in order not to be confused with the commands that are written with lower case characters. A number with a letter is considered as an hexadecimal number: dc <<< 'Ap' 10 The output is converted in base 10 by default 3 Scale And Base ~~~~~~~~~~~~~~~~ dc is a calulator with abitrary precision, by default this precision is 0. thus dc <<< "5 4/p" prints 1. We can increase the precision using the k command. It pops the value at the top of the stack and use it as the precision: dc <<< '2k5 4/p' # prints 1.25 dc <<< '4k5 4/p' # prints 1.2500 dc <<< '100k 2vp' 1.4142135623730950488016887242096980785696718753769480731766797379907\ 324784621070388503875343276415727 You can use a very large precision. You can change the base used to output (print) the numbers with o and the base used to input (type) the numbers with i: dc << EOF 20 p# prints 20 output is in base 10 16o # the output is now done in base 16 20p # prints 14, in hex 16i # the output is now done in hex p # prints 14 this doesn't modify the number present on the stack 10p # prints 10 the output is done in base 16 EOF Take care that when the input value is modified the base is modified for all the commands, including i: dc << EOF 16i 16o # base is 16 for input and output 10p # prints 10 10i # ! set the base to 10 in base 16 ie to 16 decimal 17p # prints 17 EOF This code print 17 while we might think that 10i revert the base back to 10 and thus the number should be converted to hex and prints 11. The problem is that 10 was typed while the base for input was 16, thus the base was set to 10 in hexadecimal, ie to 16 decimal. dc << EOF 16o16o10p #prints 10 Ai # set the base to A in hex ie 10 17p # prints 11 in base 16 EOF 4 Stack ~~~~~~~ There are 2 basic commands to manipulate the stack: d which duplicates the top of the stack and the c which clears the stack: $ dc << EOF 2 # put 2 on the stack d # duplicate ie put another 2 on the stack *p # multiply and print c p # clear and print EOF 4 dc: stack empty 'c p' results in an error, as we would expect, as c remove everything on the stack. Note how we can use # to put comments in the script. If you are lost you can inspect (ie print) the stack using the command f. this command does not modify the stack: dc <<< '1 2 d 4+f' 6 2 1 Note how the first element that will be poped from the stack is printed first, if you are used to HP calculator, it's the opposite. Don't hesitate to put some 'f' in the examples of this tutorial, it doesn't change the result and it's a good way to see what's going on. 5 Registers ~~~~~~~~~~~ The gnu dc manual says that dc has at least 256 registers depending on the range of unsigned char. I'm not sure how you are supposed to use the null byte ..but anyway... Using a register is easy: dc <a will execute the macro stored in 'a', if the top of the stack is greater than the second element of the stack. Take care, that the top of the stack is the number entered last so when written it's somehow the reverse of what we are used to read: dc << EOF [[Hello World]p] sR # store in 'R' a macro that prints Hello World 2 1 >R # do nothing 1 is at the top 2 is the second element 1 2 >R # prints Hello World EOF Some dc have >R R 1 2 >R f" doesn't print anything) Have you noticed how we can include a macro (string) in a macro? and as dc relies on a stack we can in fact use the macro recursively (have your favorite control-c key combo ready ;)) : dc << EOF [ [Hello World] p # our macro starts by printing Hello World lRx ] # and then executes the macro in R sR # we store it in the register R lRx # and finally executes it. EOF We have recursivity we have test, we have loops !: dc << EOF [ li # put our index i on the stack p # print it, to see what's going on 1 - # we decrement the index by one si # store decremented index (i=i-1) 0 li >L # if i > 0 the execute L ] sL # store our macro with the name L 10 si # let's give to our index the value 10 lLx # and start our loop EOF Of course code written this way is far too easy to read! Make sure to remove all those extra spaces newlines and comments: dc <<< '[lip1-si0li>L]sL10silLx' dc <<< '[p1-d0 Date: 2007/10/03 01:15:27 PM Written for http://bash-hackers.org/wiki/doku.php?id=howto:calculate-dc