CLAC(1)
π CLAC(1) β the command line, stack-based calculator with postfix notation β¨
okay so picture this: a calculator, but make it iconic. That's clac, bestie β a stack-based calculator with postfix notation that lives in your terminal, and yes this is section 1 of the manual, we're doing this properly π
πΈ NAME πΈ
clac β a command line, stack-based calculator with postfix notation. Literally that's it, that's the whole vibe. π¦
β¨ SYNOPSIS β¨
clac [expression]
π DESCRIPTION π
clac is a command line, stack-based calculator with postfix notation that displays the stack contents at all times. The way it just... shows you everything, constantly? Obsessed. As you type, stack changes are reflected immediately. No cap, you literally watch the math happen. π
So here's the tea on how a stack-based postfix calculator works: entering a number pushes it on a stack, and arithmetic operations pop their arguments from the stack and push the result. Since every operation takes a fixed number of arguments, there's zero room for ambiguity β parentheses and operator precedence are simply not needed. Postfix notation is also known as reverse Polish notation, or RPN. iykyk π
π«Ά Non-interactive mode
If an expression is provided, clac will process it and print each element in the stack starting from the top. It will then exit immediately. Boom, done, bye. β¨
π Commands
When a command requires an argument, it pops a value from the stack. If the stack is empty, a zero is provided instead β yes, a lil freebie zero, no big deal. π
In the descriptions below, the top of the stack (and thus the first value popped) is represented by the letter a, while the second value popped is represented by the letter b. For example, if the stack is composed of the numbers 1, 2 and 3 (with 3 at the top of the stack), when we describe the sum then a will be 3 and b will be 2.
β οΈ It is important to note that subtraction and division invert the order of the arguments before performing the operation: with1,2and3in the stack, when you type-it will pop the values3and2and push the result of2 - 3. This is in the tradition of other postfix calculators and programming languages. Not a bug, it's heritage. π
A description of the available commands follows. Buckle up. π
π₯ Arithmetic operations
+β Pop two valuesaandband push the result ofa + b.-β Pop two valuesaandband push the result ofb - a.*β Pop two valuesaandband push the result ofa * b./β Pop two valuesaandband push the result ofb / a.
π§ Modulo operation
%β Pop two valuesaandband push the remainder of the Euclidean division ofbbya.
β Exponentiation
^β Pop two valuesaandband push the result ofb ^ a.expβ Pop the valueaand push the result ofe ^ a.
π Logarithm
lnβ Pop the valueaand push its natural logarithm.logβ Pop the valueaand push its logarithm to base 10.
π Factorial
!β Pop the valueaand push its factorial.
π¦ Trigonometry
sinβ Pop the valueaand push its sine.cosβ Pop the valueaand push its cosine.tanβ Pop the valueaand push its tangent.asinβ Pop the valueaand push its arc sine.acosβ Pop the valueaand push its arc cosine.atanβ Pop the valueaand push its arc tangent.atan2β Pop two valuesaandband push the arc tangent ofb / a, using the signs ofaandbto determine the quadrant. Look at it being all thorough π«Ά
π Error function
erfβ Pop the valueaand push its error function.
πΈ Summation
sumβ Pop all the values in the stack and push their sum.addβ Pop the valueaand remove that many items from the stack. Push their sum.
π Product
prodβ Pop all the values in the stack and push their product.mulβ Pop the valueaand remove that many items from the stack. Push their product.
β¨ Rounding
ceilβ Pop the valueaand push the smallest integer value greater than or equal toa.floorβ Pop the valueaand push the largest integer value less than or equal toa.roundβ Pop the valueaand push the integer value closest toa.
π Absolute value
absβ Pop the valueaand push the non-negative value ofa.
π Binary operations
andβ Pop two valuesaandband push the binary AND ofaandb.orβ Pop two valuesaandband push the binary OR ofaandb.xorβ Pop two valuesaandband push the binary XOR ofaandb.
π Stack manipulation
swapβ Pop two valuesaandband push the valuesa,b.dupβ Pop the valueaand push the valuesa,a.rollβ Pop two valuesaandband rotatebelements in the stackatimes.dropβ Remove the top of the stack. bye bestie πclearβ Remove all the elements in the stack.countβ Push the number of items in the stack._β Push on the stack the result of the last operation. your little memory-keeper π₯Ί
π§Ί Stashing
stashβ Pop the valueaand move that many items to the stash.fetchβ Pop the valueaand move that many items from the stash..β Stash the top of the stack.,β Fetch one stashed item.:β Stash all the items in the stack.;β Fetch all stashed items.
π°οΈ History
Use C-p / C-n or Up / Down to navigate the history. the nostalgia trip we deserve π
π User defined operations
It is possible to define operations (or words, as they are usually called in stack based programming languages) by editing the words configuration file. It is not created by default, but clac will use some environment variables in order to search for word definitions.
$CLAC_WORDSβ If set, it should point to a file containing word definitions.$XDG_CONFIG_HOMEβ If set,clacwill search for$XDG_CONFIG_HOME/clac/words$HOMEβ If set,clacwill search for$HOME/.config/clac/words
π¨ How to define words
Words are defined as aliases, with one alias on each line. Empty lines are ignored. Here are some examples:
pi 3.14159265358979323846
tau "pi 2 *"
sqrt "0.5 ^"
Note that an alias has two parts: a word to be defined and its meaning. That's why the tau and sqrt definitions are enclosed in double quotes. If the double quotes are removed, clac will complain that it can't parse the command. For example, if we remove the double quotes from sqrt and start clac, we will get this error message:
Incorrect definition: sqrt 0.5 ^
(~/.config/clac/words:3)
User defined words can be used as if they were built-in commands:
$ clac "42 dup * pi *"
5541.76944093239
π Comments
Any lines that begin with # are considered comments and are ignored. There are no inline comments, and any # characters that are not the first character of a line are interpreted literally. To define # as a word, wrap it in double quotes in the definition:
# This is a comment.
"#" count
The first line is ignored, and the second line assigns # to the operation count. sneaky but valid π
π How to list defined words
If you type words and hit enter, clac will list the defined words. show me the roster bestie π«Ά
π How to reload defined words
If you type reload and hit enter, clac will reload the words file. refresh, reset, rebrand β¨
π EXAMPLES π
While the most interesting aspect of clac is the ability to visualize the stack as it is updated with each key press, at some point you may want use clac just to get a quick result or call it from a script. For that reason, clac can be used in non-interactive mode by invoking it with an argument.
Here are some examples of non-interactive invocations:
$ clac "3 4 +"
7
$ clac "2 3 4 +"
7
2
When clac finishes evaluating the expression "2 3 4 +", there are two elements in the stack: the number 7 at the top of the stack and the number 2 at the bottom of the stack. The elements are printed in order, one per line, starting from the top of the stack. top-down, always, no exceptions π
This other example uses the stashing features. Let's say we want to push two numbers and get the result of their multiplication plus the square of the second number.
$ clac "4 3 dup dup * . * , +"
21
Another example that uses the stash would be to get the average of all the elements in the stack:
$ clac "1 2 3 4 count . sum , /"
2.5
In fact, if you find yourself calculating averages very often, you can define the word avg as "count . sum , /". reuse, recycle, slay π§
π«Ά AUTHOR π«Ά
Michel Martens <mail@soveran.com> β ty for the calculator king π
and that's the whole page!! π this gorgeous thing is from May 3, 2017 and it's the CLAC(1) page, so like... go forth and calculate, bestie. mwah πβ¨π
π¦ where this page comes from
- nixos 26.05: clac 0.3.5
- nixpkgs unstable: clac 0.3.5
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms