Search

Affichage des articles dont le libellé est multiplication. Afficher tous les articles
Affichage des articles dont le libellé est multiplication. Afficher tous les articles

mercredi 1 mars 2017

Calc tool

Context

Let's implement a calculation tool. Inputs will be operations as [operand 1][operator][operand 2] and output will be the operation with its result

Initial state

  • Memory: empty
  • Cursor: first cell
  • Input: an operation [operand][operator][operand] where operatands are numbers in base 10, and operator one of + - * /

Process

  • Read input - memory after reading should be operand operator_flag operand 0 0 0 0 ....
    • Print char (faster than rebuilding it afterwards)
    • Use a switch/case
      • If it's a *, set operator flag to 0 (do nothing) and start reading a new integer
      • Same for +, - and * but set operator flag to 1, 2 or 3
      • Otherwise, it's a digit: build integer
        • multiply previously read integer by 10
        • add the new digit
  • Move operator to have operand 0 operand operator flag switch_flag
  • Set switch_flag to 1 and use another switch/case
    • Flag is 0: multiply numbers and store result in second cell
    • Flag is 1 or 2: same, but sums up or subtract numbers
    • Flag is 3: move operand 2, then perform division
  • Display = symbol
  • Display result
    • If it's 0, then display '0'
    • Otherwise, display the computed integer

Code - try it

Codes:
Multpily 42
Add 43
Subtract 45
Divide 47

>,[
  print char
  .
  subtract 42
  >++++++[-<------->]+<
  [
    not a multiplication
    -
    [
      not an addition
      --
      [
        not a subtraction
        --
        [
          not a division
          this is a digit
          build number
          ->++++++++[-<<[->+>>+<<<]>>>[-<<<+>>>]<]<[-<+>]
        ]>[-
          this is a division
          set operation flag to 3 and start new number
          <+++>>>
        ]<
      ]>[-
        this is a subtraction
        set operation flag to 2 and start new number
        <++>>>
      ]<
    ]>[-
      this is an addition
      set operation flag to 2 and start new number
      <+>>>
    ]<
  ]>[-
    this is a multiplication
    start new number
    >>
  ]
  read next char
  <,
]
move operator
<<[->>+<<]>>
>+<[
  not a multiplication
  -
  [
    not an addition
    -
    [->-<
      division
      move operand 2
      <[->>+<<]
      divide
      <<[->+>>+>-[<-]<[->>+<<<<[->>>+<<<]>]<<]
      clear and move result
      >[-]>>>[-]>[-<<<<+>>>>]<<
    ]>[-
      subtraction
      <<<<[->+<]>>[-<->]>>
    ]<
  ]>[-
    addition
    <<[-<+>]<<[->+<]>>>>
  ]<
]>[-
  multiplication
  <<<<[->>[-<+>>+<]>[-<+>]<<<]
  clean operands
  >>[-]>>
]

print =
+++++++++[-<+++++++>]<--.[-]

<+<[>-<
  print result if not 0
  [>>>>++++++++++<<<<[->+>>+>-[<-]<[->>+<<<<[->>>+<<<]>]<<]>+[-<+>]>>>[-]>[-<<<<+>>>>]<<<<]<[>++++++[<++++++++>-]<-.[-]<]
]>[
  or print 0
  +++++++[-<++++++>]<.[-]
]

Final state

  • Memory: empty
  • Cursor: second cell
  • Input: empty
  • Output: the operation followed by equal sign and its result
Note: of course, this is a calc tool valid in the Z/256Z group, meaning that all inputs and results are taken modulo 256. Moreover, the division is actually an euclidean division, so result is not displayed completely (only the quotient is)

mercredi 15 février 2017

Shifts

Context


Right or left bit shifts are operators in algorithmic, with a general syntax >> or <<.
These operators append B bits at the beginning or end of A, and therefore truncate the last or first B bits of A.
Example: 47 << 2 = 188, as 47 is 00101111, and 10111100 = 188 in base 10.
One useful application is the multiplication / division by powers of 2. Actually, X<<Y = X*2^Y, and X>>Y = X/2^Y.
Let's implement operations <<1 and >>1, namely multiplication / division by 2.

Initial state

  • Memory: X 0 0
  • Cursor: first cell
  • Input: any

Process - left shift

  • While first cell is not null
    • Remove 1 to first cell
    • Add 2 to second cell
    • Loop invariant: first cell + (second cell) / 2 = X
    • When first cell is null: second cell equals 2X

Code - try it

[->++<]

Final state - left shift

  • Memory: 0 2X
  • Cursor: first cell
  • Input: unchanged
  • Output: unchanged

Process - right shift

  • Division by 2 can be implemented in a different way than regular division:
  • While first cell if not null
    • Remove 1 to first cell
    • If possible, remove 1 again to first cell, increase third cell and stay on second
    • Move right (i.e. on second cell if first is now null, third otherwise, and third is not null in this case)
    • Move left if current is not null (i.e. fallback on second cell in all case)
    • Loop invariant: first cell + 2*third cell = X or X-1
    • When first cell is null: third is half X, rounded to floor

Code - try it

[-[->>+<]>[<]<]

 Final state - right shift

  • Memory: 0 0 X/2
  • Cursor: first cell
  • Input: unchanged
  • Output: unchanged



mercredi 8 février 2017

Read decimal number

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

Example

Live 'Decimal to ASCII' converter: type a number (e.g. 122) to see the corresponding ASCII char in output (e.g. 'z')