dc, an arbitrary precision calculator - 4. Arithmetic

Go to the first, previous, next, last section, table of contents.

# 4. 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 is enough to be exact.
``-'`
  Pops two values, subtracts the first one popped from the second one popped, and pushes the result.
``*'`
  Pops two values, multiplies them, and pushes the result. The number of fraction digits in the result is the largest of the precision value, the number of fraction digits in the multiplier, or the number of fraction digits in the multiplicand; but in no event exceeding the number of digits required for an exact result.
``/'`
  Pops two values, divides the second one popped from 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 that computed by the sequence `Sd dld/ Ld*-` .
``~'`
  Pops two values, divides the second one popped from 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.) (This command is a GNU extension.)
``^'`
  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 the result may not be accurate if the modulus is not 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, unlike `^`, this command will work with arbritrarily large exponents. (This command is a GNU extension.)
``v'`
  Pops one value, computes its square root, and pushes that. The precision value specifies 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.

Go to the first, previous, next, last section, table of contents.