It's a stack calculator for the unwise. Public Domain.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

7.8KB

-- DC --

desktop calculator implementation

Copyright 2022, 2023 Emil Williams, see LICENSE

GMP based DC. Uses readline. No limitations, either.

-- Summary --

DC is a reverse-polish desk calculator which supports unlimited precision arithmetic. Normally DC reads from the
standard input; if any command arguments are given to it, they are filenames, and DC reads and executes the contents of
the files instead of reading from standard input.

A reverse-polish calculator stores numbers on a stack. Entering a number pushes it on the stack. Arithmetic operations
pop arguments off the stack and push the results.

To enter a number in DC, type the digits, with an optional decimal point. To enter a negative number, begin the number
with `_'. `-' cannot be used for this, as it is a binary operator for subtraction instead. To enter two numbers in
succession, separate them with spaces or newlines. These have no meaning as direct commands. Exponential notation is not
supported.

A-F are respected as values under the circumstance that the input radix is higher than their given number.

-- Invocation --

DC may be invoked with the following command-line options:

`-e expr'
`-e=expr'
`--expression expr'
`--expression=expr'
Evaluates a DC expression.

`-f file'
`-f=file'
`--file file'
`--file=file'
Reads and evaluates expressions from a file.

`-h'
`--help'
Print a usage message summarizing the command-line options, then exits.

`-V'
`--version'
Print the current version for DC, then exits.

The envoriment is reset after every command-line entry.

-- Printing Commands --

`p'
Prints the value on the top of the stack, without altering the stack. A newline is printed after the value.

`n' (Supported GNU extension)
Prints the value on the top of the stack, popping it off, and does not print a newline after.

`f'
Prints the entire contents of the stack. This is a good command to use if you are lost or want to figure out what the
effect of some command has been.

-- Arithmetic --

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.

`/'
Pops two values, divides the second one popped from the first one popped, and pushes the result.

`%'
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*- .

`~' (Supported GNU extension)
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 sequence SdSn lnld/ LnLd% could also accomplish this function, with slightly different
error checking.)

`^'
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.

FIXME reread and rephrase truness of secondary expression always working or not.
`|' (Supported GNU extension)
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.

`v'
Pops one value, computes its square root, and pushes that.


Most arithmetic operations are affected by the precision value, which you can set with the `k' command. the default
precision value is stated within config.h and is usually 128, this refers to bit complexity and not a number of digits.

-- Additional Mathematical Functions --

(These are all my extensions.)

`@'
Pops one value, computes its absolute value, and pushes that.

`\"'
Pops one value, ceils the value, and pushes that.

`''
Pops one value, floors the value, and pushes that.

-- Stack Control --

`c'
Clears the stack, rendering it empty.

`d'
Duplicates the value on the top of the stack, pushing another copy of it. Thus, `4d*p' computes 4 squared and prints
it.

`r' (Supported GNU extension)
Reverses the order of (swaps) the top two values on the stack.

`R' (Supported GNU extension)
Rotates the top N items in a cyclical order, negatives do this in reverse.

-- Registers --

Under DC_COMPLY, DC provides at least 256 memory registers, each named by a single character. You can store a number in
a register and retrieve it later. Without DC_COMPLY there are only 95 registers, being any character between ' ' and '~'
(inclusive.)

`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. This does not alter the contents of r. Each register also
contains its own stack. The current register value is the top of the register's stack.

`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.

`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.

-- Params --

NOTE THAT INCOMPATIBILITIES EXIST WITHIN SOME OF THESE FEATURES, ESPECIALLY THE OUTPUT RADIX, WHICH DOES NOTHING.

DC has three parameters that control its operation: the precision, the input radix, and the output radix. 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. 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.

`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.

-- Status Inquiry --

`Z'
Pops a value off the stack, calculates the number of digits it has and pushes that number.
`X'
Pops a value off the stack, calculates the number of fraction digits it has, and pushes that number.
`z'
Pushes the current stack depth: the number of objects on the stack before the execution of the `z' command.

-- Misc --

`!'
Will run the rest of the line as a system command. Note that parsing of the !<, !=, and !> commands take precidence, so
if you want to run a command starting with <, =, or > you will need to add a space after the !.

`#' (Supported GNU extension)
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.

-- COMPILING --

This project uses GNU Make.

Building options:

DEBUG[=1]
Enables the debugging detail.

SAN[=...]
Adds a given sanitization.