{"id":10956,"date":"2025-06-23T02:00:00","date_gmt":"2025-06-23T06:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=10956"},"modified":"2025-06-20T14:35:34","modified_gmt":"2025-06-20T18:35:34","slug":"a-beginners-guide-to-dc","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=10956","title":{"rendered":"A beginner&#8217;s guide to dc"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"10956\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>Imagine it&#8217;s the 1970s and you&#8217;re writing a program to simulate a physical event. (This was a common use case for computers at the time, especially using the FORTRAN programming language.) Your subroutine requires using a tricky constant value, which you&#8217;ve worked out using algebra as the <em>square root of 480 divided by 3<\/em>.<\/p>\n\n\n\n<p>You <em>could<\/em> enter that calculation into the program, and let the FORTRAN program calculate <code>SQRT(480.)\/3.<\/code> each time. But calculating a square root can be slow, especially on 1970s hardware. It will be faster to enter that value as a constant in the program itself. How would you calculate the <em>square root of 480 divided by 3<\/em> so you can enter that as a <em>number<\/em> in the program?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"old-style-calculators\">Old-style calculators<\/h2>\n\n\n\n<p>In 2025, you&#8217;d probably use the calculator app on your phone. Before the era of the smartphone, you&#8217;d probably use a pocket calculator, like the Texas Instruments TI-36X calculator that I used throughout my undergraduate physics program in the 1990s.<\/p>\n\n\n\n<p>But in the 1970s, the &#8220;pocket calculator&#8221; was an expensive device; the Hewlett Packard <a href=\"https:\/\/en.wikipedia.org\/wiki\/HP-35\">HP-35 calculator<\/a> was introduced in the early 1970s and cost $395 (that&#8217;s over $3000 in today&#8217;s dollars, which is quite an investment). Pocket calculators became less expensive over time, such as the mid to late 1970s, although less expensive scientific pocket calculators became available later, around the late 1970s to early 1980s.<\/p>\n\n\n\n<p>Instead, a programmer in the 1970s would likely use a <em>desktop calculator<\/em> to calculate the <em>square root of 480 divided by 3<\/em>. A desktop calculator was a big machine that sat on your desk (hence its name) and was dedicated to performing all kinds of calculations. These were still expensive, but less so than a pocket scientific calculator.<\/p>\n\n\n\n<p>Calculators at the time typically used <em>Reverse Polish Notation<\/em> (RPN) where you entered numbers separately, <em>then<\/em> pressed a key to perform an operation. Entering values was done on a <em>stack<\/em>, which was &#8220;first in, last out.&#8221; That is, adding two numbers like <strong>2+4<\/strong> required entering the value <strong>2<\/strong> into the stack (called &#8220;pushing&#8221;), then entering the value <strong>4<\/strong> into the stack, then pressing the &#8220;+&#8221; key. This took the two most recent values (<strong>2<\/strong> and <strong>4<\/strong>) off the stack (called &#8220;popping&#8221;) and added them, then pushed the answer (<strong>6<\/strong>) back onto the stack.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-desktop-calculator-dc\">The desktop calculator: dc<\/h2>\n\n\n\n<p>In the early 1970s, one of the first programs written for the PDP-11 at Bell Labs was <strong>dc<\/strong>, the &#8220;desktop calculator&#8221; program created by Lorinda Cherry and Robert Morris. It is also one of the oldest original Unix programs, and it&#8217;s still available on Linux today. I mostly use <strong>dc<\/strong> to perform quick calculations while I&#8217;m at the command line.<\/p>\n\n\n\n<p>Not surprisingly, <strong>dc<\/strong> uses RPN like every other calculator from the 1970s. This may seem strange if you aren&#8217;t used to RPN, but it gets easier after you&#8217;ve used it for a while. The key to using RPN in <strong>dc<\/strong> is to enter each number and operation, surrounded by whitespace like a space, tab, or new line. For example, to calculate <strong>2+4<\/strong>, you push the values <strong>2<\/strong> and <strong>4<\/strong> onto the stack, then enter &#8220;+&#8221; to add them. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ dc\n2\n4\n+<\/code><\/pre>\n\n\n\n<p>This pops <strong>2<\/strong> and <strong>4<\/strong> off the stack, adds them, and pushes <strong>6<\/strong> back onto the stack. But you&#8217;ll notice that <strong>dc<\/strong> doesn&#8217;t print the result. That&#8217;s because when <strong>dc<\/strong> was created, Bell Labs used the TeleType terminal, which printed everything on paper (not a screen). And printing on a TeleType is loud and slow. If <strong>dc<\/strong> didn&#8217;t need to print something, it didn&#8217;t. The user might have more calculations to do with this value, so it doesn&#8217;t print the result until you use the &#8220;p&#8221; command. Let&#8217;s add the &#8220;p&#8221; command to the session, but this time I&#8217;ll enter the values and operation on the first line, to show you can use any whitespace to separate the values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ dc\n2 4 +\np\n6<\/code><\/pre>\n\n\n\n<p>If that&#8217;s the only calculation you need to do, you can quit the program by entering the &#8220;q&#8221; command, or pressing <em>ctrl+D<\/em> on the keyboard.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sample-calculations-with-dc\">Sample calculations with dc<\/h2>\n\n\n\n<p>Let&#8217;s do a few sample calculations with <strong>dc<\/strong> to see how it works. Remember, to see the results, you need to add the &#8220;p&#8221; command, which I&#8217;ll include.<\/p>\n\n\n\n<p>We&#8217;ve already seen an example of addition, so let&#8217;s start with subtraction. To subtract 7 from 72, you push 72 and 7 onto the stack, <em>in that order<\/em>, then use the &#8220;-&#8221; command to subtract them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>72 7 -\np\n65<\/code><\/pre>\n\n\n\n<p>Printing a value with &#8220;p&#8221; doesn&#8217;t change the stack, which now contains only the value 65. If we needed to subtract another number from this value, such as <strong>65-12<\/strong>, we can push the number 12 onto the stack, then use the &#8220;-&#8221; command to subtract 12 from 65:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>12 -\np\n53<\/code><\/pre>\n\n\n\n<p>If we needed to subtract two <em>different<\/em> numbers, such as <strong>22-9<\/strong>, we can enter those two values into the stack and use the &#8220;-&#8221; command again. The stack will actually have <em>three<\/em> values in it (53, 22, and 9) but the basic arithmetic commands (&#8220;+&#8221;, &#8220;-&#8220;, &#8220;<code>*<\/code>&#8221; and &#8220;\/&#8221;) only operate on the <em>two most recent entries<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>22 9 -\np\n13<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"values-on-the-stack\">Values on the stack<\/h2>\n\n\n\n<p>After the &#8220;-&#8221; command, the stack now has two values in it: 53 (left over from the previous calculation) and 13 (from this calculation). If you want to see the contents of the stack, use the &#8220;f&#8221; command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f\n13\n53<\/code><\/pre>\n\n\n\n<p>These values are actually shown in <em>reverse<\/em> order, which we can see if we use &#8220;-&#8221; one more time. This will subtract 13 from 53.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- p\n40\n<\/code><\/pre>\n\n\n\n<p>We can use the &#8220;f&#8221; command one more time to show that only one value is left in the stack after subtracting these two values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f\n40<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"adjusting-the-precision\">Adjusting the precision<\/h2>\n\n\n\n<p>Let&#8217;s say you want to divide two values, such as 42 by 6. You use the &#8220;\/&#8221; command to divide the two most recent entries in the stack. I&#8217;ll start this session with the &#8220;c&#8221; command to clear the stack, and &#8220;f&#8221; to show that there&#8217;s nothing in there, then I&#8217;ll enter and print the division calculation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c\nf\n42 6 \/\np\n7<\/code><\/pre>\n\n\n\n<p>This result is as you would expect, because 6 times 7 <em>really is<\/em> the value 42. The division is even, without any remainder. But what happens when you divide two numbers that don&#8217;t produce a nice value like that? Let&#8217;s try it by dividing 1 by 4, which should give 0.25:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c\n1 4 \/\np\n0<\/code><\/pre>\n\n\n\n<p>Oh no! That gives a zero value. This is because <strong>dc<\/strong> is an <em>arbitrary precision<\/em> calculator, and you can adjust the precision to any number of decimal places. By default, the precision is zero, which works only for integer values. To set the precision to some other value, like 5 decimal places, enter the number 5 into the stack and use the &#8220;k&#8221; command. If you repeat the calculation, you&#8217;ll get the expected result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5 k\n1 4 \/\np\n.25000<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"calculating-with-dc\">Calculating with dc<\/h2>\n\n\n\n<p>To get started with <strong>dc<\/strong>, I recommend you stick to the basics. These basic operations are enough to solve the calculations that I need to do on a day-to-day basis:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>operation<\/th><th>what it does<\/th><\/tr><\/thead><tbody><tr><td><code>+<\/code><\/td><td>Addition<\/td><\/tr><tr><td><code>-<\/code><\/td><td>Subtraction<\/td><\/tr><tr><td><code>*<\/code><\/td><td>Multiplication<\/td><\/tr><tr><td><code>\/<\/code><\/td><td>Division<\/td><\/tr><tr><td><code>%<\/code><\/td><td>Modulo (remainder after division) such as <code>13 4 %<\/code> to calculate <code>1<\/code><\/td><\/tr><tr><td><code>^<\/code><\/td><td>Exponent, such as <code>4 2 ^<\/code> to calculate <em>4 squared<\/em><\/td><\/tr><tr><td><code>v<\/code><\/td><td>Square root (you might also need to set the precision)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Use these commands to interact with the calculator, such as to clear the stack or manipulate values. Again, as you get started with <strong>dc<\/strong>, keep it to the basics:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>command<\/th><th>what it does<\/th><\/tr><\/thead><tbody><tr><td><code>c<\/code><\/td><td>clear the stack<\/td><\/tr><tr><td><code>d<\/code><\/td><td>duplicate the value on the stack, such as <code>3 d *<\/code> to calculate <em>3 squared<\/em><\/td><\/tr><tr><td><code>r<\/code><\/td><td>reverse the two &#8220;top&#8221; entries on the stack<\/td><\/tr><tr><td><code>k<\/code><\/td><td>set the precision (number of decimal places)<\/td><\/tr><tr><td><code>q<\/code><\/td><td>quit<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>So let&#8217;s go back to the original problem: calculate the <em>square root of 480 divided by 3<\/em>. That is not a round number, so we should first set the precision with the &#8220;k&#8221; command. Calculate the square root of 480, divide that result by 3, then print the result. I&#8217;ll group the calculations and commands on separate lines, but these could all be entered on separate lines or all on one line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ dc\n8 k\n480 v\n3 \/\np\n7.30296743<\/code><\/pre>\n\n\n\n<p>Let&#8217;s verify that result: if I enter that calculation into a calculator app on my phone, the square root of 480 is about 21.9089023. And 21.9089023 divided by 3 is 7.3029674334.<\/p>\n\n\n\n<p>The <strong>dc<\/strong> command is a handy tool to use when you are working at the command line and need to calculate a value quickly. The calculator app on my phone, or the calculator app on my desktop system, is convenient most of the time\u2014but if I&#8217;m working at the command line and just need to calculate something, pulling up a separate app is a distraction. Using <strong>dc<\/strong> keeps me at the command line, where I&#8217;m already doing my work.<\/p>\n\n\n\n<p>I&#8217;ve only covered the basics of <strong>dc<\/strong> here. If you want to do more with it, use <strong>man dc<\/strong> to explore the manual page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn the basics of this handy command line calculator.<\/p>\n","protected":false},"author":33,"featured_media":9839,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[100,5],"tags":[104,91],"class_list":["post-10956","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-linux","tag-command-line","tag-linux"],"modified_by":"Jim Hall","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/10956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10956"}],"version-history":[{"count":3,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/10956\/revisions"}],"predecessor-version":[{"id":10959,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/10956\/revisions\/10959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/9839"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}