Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Is the number before, in or after the interval? using spaceship operator <=>

by LanX (Archbishop)
on Nov 21, 2014 at 00:44 UTC ( #1107974=note: print w/replies, xml ) Need Help??


in reply to Is the number before, in or after the interval? using spaceship operator <=>

> It can be find using two basic comparisons.

Strange, I count 6 operations! :)

floor (($i - $l ) / ($r - $l +1 )) <=> 0 1 2 3 4 5 6

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

PS: Please don't post such stuff in SOPW without a clear question, you are keeping others busy moderating that stuff:

  • Tricks belong to Cool Uses For Perl
  • Opinions belong to Meditations
    • Comment on Re: Is the number before, in or after the interval? using spaceship operator <=>
    • Download Code
  • Replies are listed 'Best First'.
    Re^2: Is the number before, in or after the interval? using spaceship operator <=>
    by rsFalse (Friar) on Nov 21, 2014 at 14:43 UTC
      Hm.. yes.
      And that is simpler solution with two basic comparisons:
      map { ("-1", " 0", " 1")[ ($_ >= $l) + ($_ > $r) ] } -9 .. 10;
         ($_ > $r) - ($_ < $l)  

        Should already do (can't test on Android :)

        update

        tested! =)

        DB<41> sub tst { ($_ > $r) - ($_ < $l) } DB<42> $l=3;$r=6 => 6 DB<43> print tst(),"\t" for 0..9 => "" -1 -1 -1 0 0 0 0 1 1 1

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: note [id://1107974]
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (6)
    As of 2019-12-14 00:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?