Shop OBEX P1 Docs P2 Docs Learn Events
>= : 'Didn't think I'd EVER use it for anything. I was wrong. — Parallax Forums

>= : 'Didn't think I'd EVER use it for anything. I was wrong.

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2010-04-06 00:28 in Propeller 1
I've publicly disparaged the choice of => for greater-than-or-equal and >= for the seemingly useless "compare the variable with the expression and assign it the result of the comparison". I mean, who would ever use that for anything? It takes a variable with a numerical type and, essentially, changes its type to Boolean.

Well, I've been working on an object for the new S2 (Scribbler II) robot and came up with a use for this obscure operator for the line sensor accessor function. (The line sensors are now analog, BTW, but there's still a need for a line/no-line detector.) Here's what I came up with:

[b]PUB[/b] line_sensor(side, threshold)

  ' If threshold => 0
  '   Return the value of the line sensor on side (LEFT or RIGHT),
  '   compared to the threshold. (If threshold is zero, substitute 64.)
  '   Return: false == dark; true == light
  ' If threshold < 0
  '   Return analog value of line sensor.

  [b]if[/b] (side == LEFT)
    [b]result[/b] := results[noparse][[/noparse]*ADC_L_LIN] >> 8
  [b]elseif[/b] (side == RIGHT)
    [b]result[/b] := results[noparse][[/noparse]*ADC_R_LIN] >> 8
  [b]if[/b] (threshold == 0)
    [b]result[/b] >= 64
  [b]elseif[/b] (threshold > 0)
    [b]result[/b] >= threshold     




Who'd have thunk it? smile.gif

-Phil

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2010-04-05 20:25
    Certainly not I!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    ·
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-04-05 21:33
    Don't ask me.
    I'm surprised that side can equal Left twice
    and that result likes the funny face with the big nose.
    It makes you wonder what life was like before Boolean.
  • localrogerlocalroger Posts: 3,452
    edited 2010-04-05 23:33
    NOOOOOooooooo........

    You have now ruined one of the most infamous of the Prop Gotchas. (Though honestly I love the fact that most of those gotchas exist because of a philosophy of really extreme consistency, which some other platforms could learn a lesson from.)
  • James NewmanJames Newman Posts: 133
    edited 2010-04-05 23:52
    I think they would make sense reversed... >= for gte and => for the compare-assign... but whatever [noparse];)[/noparse]
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-04-06 00:26
    James newman said...
    I think they would make sense reversed... >= for gte and => for the compare-assign...
    Then you'd have to change all the other assignments (e.g. =+, =/, even =[noparse]:)[/noparse]. Maybe that wouldn't be so bad: assignments could then take place from left to right and be left-associative like all the other operators. Now that would be consistency at its most extreme!

    a + b =: a
    
    
    


    -Phil
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-04-06 00:28
    Complex assignment operators end with =, hence the "backward" comparison operators.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
Sign In or Register to comment.