Code patternsInput and outputRead a decimal number
Read a decimal number
Reads digits such as 173 and puts the number they spell in a cell.
Watch it run
The form “A line of digits” works on 3 cells, named “number”, “letter” and “scratch”. Press Play, drag the step slider forwards and back, or change the number typed with the slider:
The figure needs JavaScript. The program below is the result for the example values.
The digits arrive first to last. For every new digit: multiply what we have by 10, and add the digit. 1, then 17, then 173.
A digit arrives as a letter code, 48 more than its value. So 48 is taken off first.
Multiplying by 10 is the pattern multiply by a constant: a flat loop, one optimised step.
The code
This is the program the figure ran. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:
>,----------[>++++++[-<------>]<--<[->>++++++++++<<]>>[-<<+>>]<[-<+>],----------]<
What it needs
- Short form: a line that ends with a newline, digits only; two scratch cells; wrapping cells.
- The form by Urban Müller also stops at the end of the input, with any of the three answers
,can give there. - A number that does not fit the cell wraps.
Common mistakes
- Feeding it anything but digits. Both forms treat every letter as a digit: "a" counts as 49.
- Typing 300 on 8-bit cells and expecting 300. The cell holds 300 − 256 = 44.
What it costs
A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:
The figure needs JavaScript. The table below has the measured steps.
Cost as a formula, and measured
| Form | Plain steps | Optimised steps |
|---|---|---|
| A line of digits | grows with the number | 4 + 9 per digit |
| The form by Urban Müller | grows with the number | 16 + 19 per digit |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| A line of digits | n = 0 | 8 | 82 | 104 | 13 |
| A line of digits | n = 7 | 8 | 82 | 139 | 13 |
| A line of digits | n = 173 | 8 | 82 | 1,887 | 31 |
| The form by Urban Müller | n = 173; ends with = a newline | 8 | 94 | 1,977 | 73 |
Other forms
The form by Urban Müller
Stops at a newline or at the end of the input. Layout: number, flag, letter, scratch.
[-]>[-]+[[-]>[-],[+[-----------[>[-]++++++[<------>-]<--<<[->>++++++++++<<]>>[-<<+>>]<+>]]]<]<
Run this form step by step
The form “The form by Urban Müller” works on 4 cells, named “number”, “flag”, “letter” and “scratch”. Press Play, drag the step slider forwards and back, or change the number typed with the slider and edit the choice of ends with:
The figure needs JavaScript. The program below is the result for the example values.
Where it is used
- With print a number in decimal it makes a calculator: see the challenges.
Sources
- Source: Esolang wiki: Brainfuck algorithms: section "Input a decimal number" (Urban Müller, 1993). The short form is our own..
Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.