Context
Given an input '123', reading input will populate cells with values [49], [50] and [51] (ASCII codes of 1, 2 and 3). Here, we want to read 123 as a number, and have one unique value [123].Therefore, we'll have to:
- Read a char
- Convert it to a digit (-48)
- Multiply the previous result by 10, and add the new digit
Initial state
- Memory: 0, 0, 0, 0
- Cursor: first cell
- Input: a decimal number
Process
- Move to second cell (first is for result)
- While a digit is read
- Convert to decimal digit (-48)
- Multiply first cell by 10 and add current digit
- Loop invariant: first cell = current read number (decimal)
- When all chars are read, first cell equals number to read
Code
>,[
>++++++[-<-------->]
remove 48
+++++++++[-<<[->+>>+<<<]>>>[-<<<+>>>]<]
add first cell 9 times to second
<[-<+>]
add second cell to first (result: first cell * 10 plus second cell)
,
read next char
]<
Code (minified)
>,[>++++++[-<-------->]+++++++++[-<<[->+>>+<<<]>>>[-<<<+>>>]<]<[-<+>],]<
Final state
- Memory: number value (decimal)
- Cursor: first cell
- Input: empty
- Output: unchanged
A number is a concept that arises from the result to have things that are an aggregate or a generalization of this concept.
RépondreSupprimernumbers information