CALC
What a scientific calculator with memory can do, if you keep pressing equals until it says Math ERROR.
OllyBritton, 2024 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 5 known programs · Test method
Run it
The idea
OllyBritton made CALC in 2024 to show that the Casio FX-991EX can do any computation, though it is described as non-programmable. A program is the kind of expression the calculator takes, with variables, in three sections. The first runs once, the second runs again and again, and the third runs once at the end.
Read the story
There are no jumps, no if and no booleans. The loop ends only on a math error, so a program ends it on purpose with something like sqrt(maxiter - iter), which fails once iter passes maxiter. A choice is made with arithmetic. delta(x, y) is 1 when x equals y and 0 otherwise, so x + (y - x) * delta(m, 2) sets x to y only when m is 2. The specification uses this to run any Fractran program, which makes CALC Turing complete.
The machine
| Piece | Does |
|---|---|
expr -> v | Assign the value to the variable v |
? -> v | Read a number into v |
::: | Start the next section; once, then a loop, then once |
: | Separate statements on one line |
# | Comment to the end of the line |
P(x) | Print x and give it back |
delta(x, y) | 1 if x equals y, else 0 |
round floor ceil | To the nearest whole number, down, up; round takes half away from 0 |
sqrt | The square root; an error below 0 |
random_int(x, y) | A whole number from x to y |
Numbers are doubles. Every result that is not a finite number is a math error, so division by 0 ends the loop as sqrt(-1) does. A variable that was never set is 0, as in the calculator's memory, and reading past the end of the input is an error too. Each printed number goes on its own line.
Example
Fibonacci up to a limit.
Fibonacci up to a limit. Each pass prints two numbers, and on the fifth pass iter becomes 6 and sqrt(5 - 6) ends the loop.
The opening example takes ten steps of Newton's method for the square root of 2 from a guess, then prints it in the third section. In doubles the steps swing between the two values nearest the root, and the tenth lands on the lower one.
The prime lister is a machine with three modes kept in B. It prints A * delta(B, 0) on every pass, so it prints 0 whenever it has no prime to show. It prints 3,359 lines, and the 25 that are not 0 are the primes below 100.
Patterns it teaches
- Turing tarpit: one loop, ended only by an error, and branches made of arithmetic.
- Turing completeness: a Fractran machine in calculator keys.
Further
See also
Also in Turing tarpits
Write your own in Run Convert a program from CALC
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: calc.
Command line
shib run calc -e '? -> input
P(input)' --input '42'shib manual calcSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/calc.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org