sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

dc.1 (5795B)


      1 .Dd January 14, 2026
      2 .Dt DC 1
      3 .Os sbase
      4 .Sh NAME
      5 .Nm dc
      6 .Nd arbitrary-precision desk calculator
      7 .Sh SYNOPSIS
      8 .Nm
      9 .Op Fl i
     10 .Op Ar file ...
     11 .Sh DESCRIPTION
     12 .Nm
     13 is a reverse-polish notation arbitrary-precision desk calculator.
     14 It reads and executes commands from each
     15 .Ar file
     16 in sequence.
     17 After processing all files,
     18 .Nm
     19 reads from stdin.
     20 .Pp
     21 Numbers are arbitrary-precision decimal values.
     22 Numbers may contain a decimal point and use
     23 .Ql _
     24 as a negative sign prefix.
     25 When the input base is greater than 10, letters A-F represent digits 10-15.
     26 .Sh OPTIONS
     27 .Bl -tag -width Ds
     28 .It Fl i
     29 Extended identifier mode.
     30 Register names can be enclosed in
     31 .Ql < Ns No ... Ns >
     32 (numeric identifiers) or
     33 .Ql \&" Ns No ... Ns \&"
     34 (string identifiers).
     35 .El
     36 .Sh COMMANDS
     37 .Ss Printing
     38 .Bl -tag -width Ds
     39 .It Ic p
     40 Print the value on top of the stack with a newline,
     41 without popping it.
     42 .It Ic n
     43 Print the value on top of the stack without a newline,
     44 and pop it.
     45 .It Ic P
     46 Print the value on top of the stack as a byte stream.
     47 For strings, print the characters directly.
     48 For numbers, print it in base 100 representing the internal representation.
     49 The value is popped.
     50 .It Ic f
     51 Print all values on the stack, one per line, from top to bottom.
     52 .El
     53 .Ss Arithmetic
     54 .Bl -tag -width Ds
     55 .It Ic +
     56 Pop two values, add them, and push the result.
     57 .It Ic \-
     58 Pop two values, subtract the top from the second, and push the result.
     59 .It Ic *
     60 Pop two values, multiply them, and push the result.
     61 .It Ic /
     62 Pop two values, divide the second by the top, and push the quotient.
     63 The scale of the result is determined by the
     64 .Ic scale
     65 parameter.
     66 .It Ic %
     67 Pop two values, compute the remainder of dividing the second by the top,
     68 and push the result.
     69 .It Ic ~
     70 Pop two values, compute both quotient and remainder,
     71 pushing the quotient first then the remainder on top.
     72 .It Ic ^
     73 Pop two values, raise the second to the power of the top, and push the result.
     74 The exponent is truncated to an integer.
     75 .It Ic v
     76 Pop the top value, compute its square root, and push the result.
     77 The precision is determined by the
     78 .Ic scale
     79 parameter.
     80 .El
     81 .Ss Stack
     82 .Bl -tag -width Ds
     83 .It Ic c
     84 Clear the stack.
     85 .It Ic d
     86 Duplicate the top of the stack.
     87 .It Ic r
     88 Reverse the order of the top two values on the stack.
     89 .It Ic z
     90 Push the current stack depth.
     91 .El
     92 .Ss Registers
     93 Registers are named storage locations.
     94 In normal mode, register names are single characters.
     95 With the
     96 .Fl i
     97 option, extended names are available.
     98 Each register also has an associated stack.
     99 .Bl -tag -width Ds
    100 .It Ic s Ns Ar x
    101 Pop the top value and store it in register
    102 .Ar x .
    103 .It Ic l Ns Ar x
    104 Push a copy of the value in register
    105 .Ar x
    106 onto the stack.
    107 .It Ic S Ns Ar x
    108 Pop the top value and push it onto register
    109 .Ar x Ns 's
    110 stack.
    111 .It Ic L Ns Ar x
    112 Pop the top value from register
    113 .Ar x Ns 's
    114 stack and push it onto the main stack.
    115 .El
    116 .Ss Arrays
    117 Each register has an associated array.
    118 .Bl -tag -width Ds
    119 .It Ic : Ns Ar x
    120 Pop an index, then pop a value, and store the value at that index in array
    121 .Ar x .
    122 .It Ic ; Ns Ar x
    123 Pop an index and push the value at that index in array
    124 .Ar x .
    125 .El
    126 .Ss Parameters
    127 .Bl -tag -width Ds
    128 .It Ic i
    129 Pop the top value and use it as the input radix.
    130 The input base must be between 2 and 16.
    131 .It Ic o
    132 Pop the top value and use it as the output radix.
    133 The output base must be at least 2.
    134 .It Ic k
    135 Pop the top value and use it as the scale
    136 .Pq number of decimal places
    137 for arithmetic operations.
    138 The scale must be non-negative.
    139 .It Ic I
    140 Push the current input radix.
    141 .It Ic O
    142 Push the current output radix.
    143 .It Ic K
    144 Push the current scale.
    145 .El
    146 .Ss Strings and Macros
    147 .Bl -tag -width Ds
    148 .It Ic \&[ Ns Ar string Ns Ic \&]
    149 Push
    150 .Ar string
    151 onto the stack.
    152 Brackets inside the string must be balanced or escaped with a backslash.
    153 .It Ic x
    154 Pop the top value.
    155 If it is a string, execute it as a macro.
    156 If it is a number, push it back.
    157 .El
    158 .Ss Conditionals
    159 The conditional commands pop two values, compare them,
    160 and if the condition is true, execute the contents of register
    161 .Ar x
    162 as a macro.
    163 .Bl -tag -width Ds
    164 .It Ic > Ns Ar x
    165 Execute register
    166 .Ar x
    167 if the second value is greater than the top.
    168 .It Ic < Ns Ar x
    169 Execute register
    170 .Ar x
    171 if the second value is less than the top.
    172 .It Ic = Ns Ar x
    173 Execute register
    174 .Ar x
    175 if the two values are equal.
    176 .It Ic !> Ns Ar x
    177 Execute register
    178 .Ar x
    179 if the second value is not greater than (less or equal to) the top.
    180 .It Ic !< Ns Ar x
    181 Execute register
    182 .Ar x
    183 if the second value is not less than (greater or equal to) the top.
    184 .It Ic != Ns Ar x
    185 Execute register
    186 .Ar x
    187 if the two values are not equal.
    188 .El
    189 .Ss Control
    190 .Bl -tag -width Ds
    191 .It Ic q
    192 Quit.
    193 If executing a macro, exit two macro levels.
    194 .It Ic Q
    195 Pop a value and quit that many macro levels.
    196 .El
    197 .Ss Input
    198 .Bl -tag -width Ds
    199 .It Ic ?
    200 Read a line from stdin and execute it.
    201 .It Ic #
    202 Comment.
    203 The rest of the line is ignored.
    204 .It Ic !\& Ns Ar command
    205 Execute
    206 .Ar command
    207 as a shell command.
    208 .El
    209 .Ss Number Information
    210 .Bl -tag -width Ds
    211 .It Ic Z
    212 Pop a value and push its length.
    213 For numbers, push the number of significant digits.
    214 For strings, push the number of characters.
    215 .It Ic X
    216 Pop a value and push its scale (number of fractional digits).
    217 For strings, push 0.
    218 .El
    219 .Sh EXAMPLES
    220 Compute 6 * 7:
    221 .Bd -literal -offset indent
    222 6 7*p
    223 .Ed
    224 .Pp
    225 Compute 2^10:
    226 .Bd -literal -offset indent
    227 2 10^p
    228 .Ed
    229 .Pp
    230 Compute square root of 2 with 20 decimal places:
    231 .Bd -literal -offset indent
    232 20k 2vp
    233 .Ed
    234 .Pp
    235 Factorial using recursion (store in register f):
    236 .Bd -literal -offset indent
    237 [d1-d1<f*]sf 10lf xp
    238 .Ed
    239 .Sh SEE ALSO
    240 .Xr bc 1
    241 .Rs
    242 .%A R. H. Morris
    243 .%A L. L. Cherry
    244 .%T "DC \(em An Interactive Desk Calculator"
    245 .Re
    246 .Sh STANDARDS
    247 .Nm
    248 is compatible with traditional UNIX dc implementations.
    249 The
    250 .Fl i
    251 flag,
    252 .Ic #
    253 comments and
    254 the
    255 .Ic ~
    256 operator
    257 are extensions to the traditional dc specification.