Search

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

Aucun commentaire:

Enregistrer un commentaire