Search

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

mercredi 15 février 2017

Evaluate GT / LT / GE / LE

Context

We already went through the EQ / NEQ evaluation, let's now see how to check, given 2 numbers A and B, if A > B or if B ≤ A
Initial state

  • Memory: A B 0 0
  • Cursor: first cell
  • Input: any

Process

  • Set else bit on fourth cell
  • While A is not null
    • Decrease A by 1
    • If B is not null
      • Decrease B by 1
      • Reset else bit
    • If B is null (i.e. if else bit is not null)
      • A is definitely greater than or equal to B: no need to continue the loop, we can reset A
    • Set else bit to 1 and go back to A
  • Do some result formatting (A is always null, B is null if and only if B ≤ A, so this would be our result)

Code - try it

set else bit
>>+<<
[
  decrease A by 1
  -
  if B is not null: decrease B and reset else bit
  >[->]>
  if B is null (else bit not null)
  [
    reset A
    <<[-]
    clear else
    >>-
    go to same position
    >>
  ]
  set else bit and back to A
  <<+<<
]

format / copy result
if B is not null then set result to true
>[[-]<+>]
reset else bit
>-<<

Code - minified

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

Final state

  • Memory: result 0 0 0 with result = 1 if and only if A < B
  • Cursor: on result
  • Input: unchanged
  • Ouput: unchanged

Note

This can be easily modified to compute GE / LT: simply set result to true before final check on B, that will do a reset instead of set. As B is not null if and only if B > A, then
  • set result to true
  • and reset if B > A
will give true for A ≥ B

mercredi 8 février 2017

Evaluate EQ / NEQ

Context

To know whether 2 values A and B are equal or not, we need to compute A - B: if the result is 0, then A=B, otherwise A<>B.
So, we can say that "A<>B" = (A-B).
Optionally, we can use some extra code to have evaluate A=B

Initial state

  • Memory: A, B
  • Cursor: first cell
  • Input: any

Process

  • While second cell is not null
    • Decrease first and second cells by 1
    • Loop invariant: first cell - second cell = A - B
    • When second cell equals 0, first cell equals A - B
  • Move to first cell

Code 

>[-<->]<

Final state

  • Memory: A <> B, 0
  • Cursor: first cell
  • Input: any
  • Output: unchanged

Note: to compute A=B, use second cell as 'else' bit, if A <> B then reset result and else bit; and finally move else bit to result position
>[-<->]+<[>-<[-]]>[-<+>]<