DC(1plan9)

โœจ NAME โœจ

dc โ€” desk calculator. yes bestie, it's a whole calculator that lives in your terminal and it's lowkey iconic ๐Ÿงฎ๐Ÿ’…


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

dc [ file ]

๐ŸŒธ DESCRIPTION ๐ŸŒธ

okay so dc is an arbitrary precision desk calculator, which means it does NOT get flustered when your numbers are absolutely unhinged in size. normally it works with decimal integers, but you can tell it what base to read in, what base to print out, and how many fractional digits to keep around. periodt.

the whole vibe of dc is a stacking (reverse Polish) calculator ๐Ÿฅž โ€” so you push numbers, then you tell it what to do with them. no parentheses drama, no precedence discourse.

if you hand it an argument, it reads input from that file until the file runs out, then it just keeps going and reads from standard input. the following constructions are recognized:

number โ€” the value of the number gets pushed on the stack. a number is an unbroken string of the digits 0-9A-F or 0-9a-f. ๐Ÿšจ important: a hexadecimal number that starts with a lower case letter has to be preceded by a zero, otherwise dc thinks it's the command for that letter. you can also slap an underscore _ in front to input a negative number. numbers may contain decimal points.

+ - / * % ^ โ€” add, subtract, multiply, divide, remainder, or exponentiate the top two values on the stack. both entries get popped off, and the result gets pushed on in their place. any fractional part of an exponent is just ignored, no cap.

sx Sx โ€” pop the top of the stack and store it into a register named x, where x can be any character. under operation S, register x is treated as a stack and the value gets pushed on it. ๐Ÿ“š

lx Lx โ€” push the value in register x onto the stack. register x is not altered. all registers start with a zero value. under operation L, register x is treated as a stack and its top value gets popped onto the main stack. ๐Ÿซถ

d โ€” duplicate the top value on the stack. it's giving "copy paste but make it math".

p โ€” print the top value on the stack, and the top value stays exactly where it is, unchanged. P interprets the top of the stack as a text string, removes it, and prints it. ๐Ÿ–จ๏ธโœจ

f โ€” print the values on the stack. the whole stack. all of it.

q Q โ€” exit the program. if you're executing a string, the recursion level is popped by two. under operation Q, the top value on the stack is popped and the string execution level is popped by that value. bye bestie ๐Ÿ‘‹๐Ÿ’€

x โ€” treat the top element of the stack as a character string and execute it as a string of dc commands. yes it runs code it found on the stack, slay.

X โ€” replace the number on the top of the stack with its scale factor.

[ ... ] โ€” put the bracketed text string on the top of the stack. ๐ŸŽ€

<x >x =x โ€” pop and compare the top two elements of the stack. register x is executed if they obey the stated relation.

v โ€” replace the top element on the stack by its square root. any existing fractional part of the argument is taken into account, but otherwise the scale factor is ignored.

! โ€” interpret the rest of the line as a shell command. ๐Ÿš (careful, bestie, that's real shell)

c โ€” clear the stack. clean slate energy โœจ

i โ€” the top value on the stack is popped and used as the number base for further input.

I โ€” push the input base on the top of the stack.

o โ€” the top value on the stack is popped and used as the number base for further output. in bases larger than 10, each 'digit' prints as a group of decimal digits. ๐Ÿ‘€

O โ€” push the output base on the top of the stack.

k โ€” pop the top of the stack, and use that value as a non-negative scale factor: the appropriate number of places are printed on output, and maintained during multiplication, division, and exponentiation. the interaction of scale factor, input base, and output base will be reasonable if all are changed together. change them as a squad, not solo ๐Ÿ’…

z โ€” push the stack level onto the stack.

Z โ€” replace the number on the top of the stack with its length.

? โ€” a line of input is taken from the input source (usually the terminal) and executed.

; : โ€” used by bc for array operations. (iykyk)


the scale factor set by k decides how many digits get kept to the right of the decimal point. if s is the current scale factor, sa is the scale of the first operand, sb is the scale of the second, and b is the (integer) second operand, then results are truncated to the following scales. ๐ŸŽฏ

+,-  max(sa,sb)
*    min(sa+sb , max(s,sa,sb))
/    s
%    so that dividend = divisor*quotient + remainder; remainder has sign of dividend
^    min(saร—|b|, max(s,sa))
v    max(s,sa)

๐Ÿ”ฅ EXAMPLES ๐Ÿ”ฅ

print the first ten values of n! โ€” look at this absolute bop:

[la1+dsa*pla10>y]sy
0sa1
lyx

๐ŸŒˆ SOURCE ๐ŸŒˆ

/src/cmd/dc.c


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

bc(1), hoc(1) โ€” the whole calculator friend group fr fr


๐Ÿšจ DIAGNOSTICS ๐Ÿšจ

  • x where x is an octal number: an internal error. ๐Ÿ˜ญ
  • Out of headers โ€” for too many numbers being kept around.
  • Nesting depth โ€” for too many levels of nested execution.

๐Ÿ’€ BUGS ๐Ÿ’€

  • when the input base exceeds 16, there is no notation for digits greater than F. so like, bestie, that's just how it is. ๐Ÿ™ˆ
  • Past its time. ๐Ÿชฆ

okay that's the whole page ๐Ÿซถ this was the dc page from the Plan 9 manual, section 1 (you know, the DC(1plan9) situation) โ€” go forth and stack some numbers, queen. ๐Ÿ’–โœจ๐ŸŽ€๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from