dc(1)

๐ŸŽ€ dc(1) โ€” ur fave arbitrary precision calculator ๐ŸŽ€

okay bestie, sit down, grab a boba, because dc is about to become ur whole personality. it lives in the General Commands Manual section of the manual pages, and it is genuinely one of the most unhinged-but-brilliant little tools ever. ๐Ÿ’…

โœจ NAME โœจ

dc โ€” an arbitrary precision calculator ๐Ÿงฎโœจ

โœจ SYNOPSIS โœจ

dc [-V] [--version] [-h] [--help]
   [-e scriptexpression] [--expression=scriptexpression]
   [-f scriptfile] [--file=scriptfile]
   [file ...]

โœจ DESCRIPTION โœจ

So dc is a reverse-polish desk calculator that does unlimited precision arithmetic โ€” like, it does NOT care how big ur numbers are, it will simply handle them. iconic behavior. ๐Ÿ‘‘

It also lets you define and call macros, which is where it gets really fun. normally dc reads from standard input. but if you give it command arguments, those are filenames, and it reads and executes the contents of those files before reading from standard input. all normal output goes to standard output; all error output goes to standard error. no cap.

A reverse-polish calculator keeps numbers on a stack. entering a number pushes it onto the stack. arithmetic operations pop arguments off the stack and push the results back. it's giving very much "trust the process." ๐Ÿซถ

Now, how do you even type a number in dc? you type the digits 0 to 9 and A through F to represent the values zero through fifteen โ€” and yes, that's regardless of the current input radix, bestie โ€” and you can optionally include a single radix point. Exponential notation is not supported, sorry. ๐Ÿ˜ญ

To enter a negative number, start the number with _. you literally cannot use - for this, because - is the binary subtraction operator. they are NOT the same bestie, do not confuse them. ๐Ÿ’€

Want two numbers in a row? separate them with spaces or newlines. those have no meaning as commands, they're just lil spacers. ๐ŸŒธ


โœจ OPTIONS โœจ

dc can be invoked with these command-line options, and honestly they're all lowkey useful:

  • -V, --version โ€” prints out the version of dc that's running plus a copyright notice, then dips out (exits). ๐Ÿ‘‹
  • -h, --help โ€” prints a usage message that briefly summarizes these command-line options and the bug-reporting address, then exits. so if you're lost, this is ur girl. ๐Ÿซถ
  • -e script, --expression=script โ€” adds the commands in script to the set of commands to be run while processing the input. ๐Ÿ“
  • -f script-file, --file=script-file โ€” adds the commands contained in the file script-file to the set of commands to be run while processing the input. ๐Ÿ“‚
  • --max-recursion=depth โ€” limits recursion to a depth of at most depth. (mainly useful for debugging. it's giving "dev tool" energy. ๐Ÿ”ง)

If any command-line parameters remain after processing all of the above, those parameters get interpreted as the names of input files to be processed. a file name of - means the standard input stream. and standard input will be processed if no script files or expressions were specified. periodt. ๐Ÿ’…


๐ŸŒธ PRINTING COMMANDS ๐ŸŒธ

  • p โ€” prints the value on the top of the stack without altering the stack. a newline is printed after the value. peek but don't touch, we love a respectful queen. โœจ
  • n โ€” prints the value on the top of the stack, popping it off, and does not print a newline after. so it just... sits there. dramatic. ๐Ÿ’€
  • P โ€” pops off the value on top of the stack. if it's a string, it's simply printed without a trailing newline. otherwise it's a number, and the integer portion of its absolute value is printed out as a "base (UCHAR_MAX+1)" byte stream. assuming that (UCHAR_MAX+1) is 256 (like it is on most machines with 8-bit bytes), the sequence
KSK0k1/_1Ss [ls*]Sxd0>x [256~Ssd0<x]dsxxsx[q]Sq[Lsd0>qaPlxx] dsxxsx0sqLqsxLxLK+k

could also accomplish this function. (much of the complexity of the above native-dc code is due to the ~ computing the characters backwards, and the desire to make sure all registers wind up back in their original states. it's a whole journey. ๐Ÿ˜ญ)

  • f โ€” prints the entire contents of the stack without altering anything. this is THE command to use if you're lost or if you wanna figure out what the effect of some command has been. a true "where am i" icon. ๐Ÿ—บ๏ธโœจ

๐Ÿ”ฅ ARITHMETIC ๐Ÿ”ฅ

  • + โ€” pops two values off the stack, adds them, and pushes the result. the precision of the result is determined only by the values of the arguments, and it's enough to be exact. no rounding, we're precise. ๐Ÿ’…
  • - โ€” pops two values, subtracts the first one popped from the second one popped, and pushes the result. order matters bestie, don't @ me. ๐Ÿ™ˆ
  • * โ€” pops two values, multiplies them, and pushes the result. the number of fraction digits in the result depends on the current precision value and the number of fraction digits in the two arguments.
  • / โ€” pops two values, divides the second one popped by the first one popped, and pushes the result. the number of fraction digits is specified by the precision value.
  • % โ€” pops two values, computes the remainder of the division that the / command would do, and pushes that. the value computed is the same as what you'd get from the sequence Sd dld/ Ld*-. math is math. ๐Ÿงฎ
  • ~ โ€” pops two values, divides the second one popped by the first one popped. the quotient is pushed first, and the remainder is pushed next. the number of fraction digits used in the division is specified by the precision value. (the sequence SdSn lnld/ LnLd% could also accomplish this function, with slightly different error checking. slay either way.) โœจ
  • ^ โ€” pops two values and exponentiates, using the first value popped as the exponent and the second popped as the base. the fraction part of the exponent is ignored. the precision value specifies the number of fraction digits in the result.
  • | โ€” pops three values and computes a modular exponentiation. the first value popped is used as the reduction modulus; this value must be a non-zero number, and should be an integer. the second popped is used as the exponent; this value must be a non-negative number, and any fractional part of this exponent will be ignored. the third value popped is the base which gets exponentiated, which should be an integer. for small integers this is like the sequence Sm^Lm%, but โ€” and this is the important part โ€” unlike ^, this command will work with arbitrarily large exponents. main character energy fr fr. ๐Ÿ‘‘
  • v โ€” pops one value, computes its square root, and pushes that. the maximum of the precision value and the precision of the argument is used to determine the number of fraction digits in the result. ๐ŸŒฑ

Most arithmetic operations are affected by the "precision value", which you can set with the k command. the default precision value is zero, which means that all arithmetic except for addition and subtraction produces integer results. so heads up if you were expecting decimals out of the box. ๐Ÿ™ˆ


๐Ÿฆ‹ STACK CONTROL ๐Ÿฆ‹

  • c โ€” clears the stack, rendering it empty. clean slate bestie. โœจ
  • d โ€” duplicates the value on the top of the stack, pushing another copy of it. so 4d*p computes 4 squared and prints it. yes it really is that cute. ๐Ÿซถ
  • r โ€” reverses the order of (swaps) the top two values on the stack. (this can also be accomplished with the sequence SaSbLaLb.) ๐Ÿ”„
  • R โ€” pops the top-of-stack as an integer n. cyclically rotates the top n items on the updated stack. if n is positive, then the rotation direction will make the topmost element the second-from-top; if n is negative, then the rotation will make the topmost element the n-th element from the top. if the stack depth is less than n, then the entire stack is rotated (in the appropriate direction), and no error is reported. she's chill like that. ๐Ÿ˜Œ

๐Ÿ’œ REGISTERS ๐Ÿ’œ

dc provides at least 256 memory registers, each named by a single character. you can store a number or a string in a register and retrieve it later. it's ur lil storage locker. ๐ŸŽ€

  • sr โ€” pop the value off the top of the stack and store it into register r.
  • lr โ€” copy the value in register r and push it onto the stack. the value 0 is retrieved if the register is uninitialized. this does not alter the contents of r. copy, don't move. ๐Ÿ’…

Each register also contains its own stack. the current register value is the top of the register's stack. so meta, so iconic. โœจ

  • Sr โ€” pop the value off the top of the (main) stack and push it onto the stack of register r. the previous value of the register becomes inaccessible. gone. ghosted. ๐Ÿ’€
  • Lr โ€” pop the value off the top of register r's stack and push it onto the main stack. the previous value in register r's stack, if any, is now accessible via the lr command. she's back from the dead. ๐ŸงŸโœจ

๐ŸŒˆ PARAMETERS ๐ŸŒˆ

dc has three parameters that control its operation: the precision, the input radix, and the output radix. let's break it down bestie:

  • the precision specifies the number of fraction digits to keep in the result of most arithmetic operations.
  • the input radix controls the interpretation of numbers typed in; all numbers typed in use this radix.
  • the output radix is used for printing numbers.

The input and output radices are separate parameters โ€” you can make them unequal, which can be useful or confusing. both. honestly both. ๐Ÿ˜ญ

  • the input radix must be between 2 and 16 inclusive.
  • the output radix must be at least 2.
  • the precision must be zero or greater.
  • the precision is always measured in decimal digits, regardless of the current input or output radix. important!! ๐Ÿšจ

Now the commands:

  • i โ€” pops the value off the top of the stack and uses it to set the input radix. ๐Ÿ”ข
  • o โ€” pops the value off the top of the stack and uses it to set the output radix. ๐Ÿ”ก
  • k โ€” pops the value off the top of the stack and uses it to set the precision. ๐ŸŽฏ
  • I โ€” pushes the current input radix on the stack. ๐Ÿ“ฅ
  • O โ€” pushes the current output radix on the stack. ๐Ÿ“ค
  • K โ€” pushes the current precision on the stack. ๐Ÿ’ซ

โœจ Strings โœจ

dc lowkey has a limited ability to operate on strings as well as on numbers. The only things you can do with strings are print them and execute them as macros โ€” and macro mode means the contents of the string get processed as dc commands. All registers and the stack can hold strings, and dc always knows whether any given object is a string or a number. Some commands, like arithmetic operations, demand numbers as arguments and print errors if given strings. Other commands can accept either a number or a string; for example, the p command can accept either and prints the object according to its type. ๐Ÿ’…

  • [characters] โ€” Makes a string containing characters (contained between balanced [ and ] characters), and pushes it on the stack. For example, [foo]P prints the characters foo (with no newline).
  • a โ€” The top-of-stack is popped. If it was a number, then the low-order byte of this number is converted into a string and pushed onto the stack. Otherwise the top-of-stack was a string, and the first character of that string is pushed back.
Note: this command is being deprecated; see dc.info for details. If you use the a command and think it should remain a part of GNU dc, file a bug report requesting its retention.
  • x โ€” Pops a value off the stack and executes it as a macro. Normally it should be a string; if it is a number, it is simply pushed back onto the stack. For example, [1p]x executes the macro 1p which pushes 1 on the stack and prints 1 on a separate line.

Macros are most often stored in registers; [1p]sa stores a macro to print 1 into register a, and lax invokes this macro. ๐Ÿซถ

  • >r โ€” Pops two values off the stack and compares them assuming they are numbers, executing the contents of register r as a macro if the original top-of-stack is greater. Thus, 1 2>a will invoke register a's contents and 2 1>a will not.
  • !>r โ€” Similar but invokes the macro if the original top-of-stack is not greater than (less than or equal to) what was the second-to-top.
  • <r โ€” Similar but invokes the macro if the original top-of-stack is less.
  • !<r โ€” Similar but invokes the macro if the original top-of-stack is not less than (greater than or equal to) what was the second-to-top.
  • =r โ€” Similar but invokes the macro if the two numbers popped are equal.
  • !=r โ€” Similar but invokes the macro if the two numbers popped are not equal.
  • ? โ€” Reads a line from the terminal and executes it. This command allows a macro to request input from the user.
  • q โ€” exits from a macro and also from the macro which invoked it. If called from the top level, or from a macro which was called directly from the top level, the q command will cause dc to exit.
  • Q โ€” Pops a value off the stack and uses it as a count of levels of macro execution to be exited. Thus, 3Q exits three levels. The Q command will never cause dc to exit.

โœจ Status Inquiry โœจ

  • Z โ€” Pops a value off the stack, calculates the number of decimal digits it has (or number of characters, if it is a string) and pushes that number. The digit count for a number does not include any leading zeros, even if those appear to the right of the radix point.
  • X โ€” Pops a value off the stack, calculates the number of fraction digits it has, and pushes that number. For a string, the value pushed is 0.
  • z โ€” Pushes the current stack depth: the number of objects on the stack before the execution of the z command.

โœจ Miscellaneous โœจ

  • ! โ€” Will run the rest of the line as a system command. Note that parsing of the !<, !=, and !> commands take precedence, so if you want to run a command starting with <, =, or > you will need to add a space after the !.
  • # โ€” Will interpret the rest of the line as a comment.
  • :r โ€” Will pop the top two values off of the stack. The old second-to-top value will be stored in the array r, indexed by the old top-of-stack value.
  • ;r โ€” Pops the top-of-stack and uses it as an index into the array r. The selected value is then pushed onto the stack.

Note that each stacked instance of a register consists of both its simple scalar value (whether a number or a string) and an array of the same name. Thus 1 0:x 0Sx 2 0:x Lx 0;xp will print 1, because the 2 was stored at index 0 in an instance of the array x which was later popped, re-exposing the instance of x that is holding a 1 at index 0.


โœจ ENVIRONMENT VARIABLES โœจ

  • DC_LINE_LENGTH โ€” Determines the column at which numeric output is split; the default value is 70. If set to ``0'' then line-splitting is disabled and each output line will be as long as necessary to represent the number.
  • DC_PROMPT โ€” Sets a prompt to use when dc is expecting interactive input on stdin.

โœจ BUGS โœจ

Email bug reports to bug-dc@gnu.org.


โœจ SEE ALSO โœจ

More complete documentation is available in dc.info, which may (depending on your local installation) be available by invoking ``info dc''.


That's the whole vibe, bestie! This manual page is from the GNU Project, dated 2025-01-05, and it's for dc(1). ๐Ÿ’–โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from