Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Range check with unordered limits

by hexcoder (Curate)
on Jul 13, 2022 at 06:44 UTC ( [id://11145463]=note: print w/replies, xml ) Need Help??


in reply to Range check with unordered limits

Dear monks, thanks for your insights and extensions!

I did this one-liner as a fun task (because i was too lazy to detect min and max values beforehand) while working with integer values.

I agree with syphilis and LanX that

$inRange = (($a <=> $x) * ($b <=> $x)) <= 0;

is the most canonical form, where <= emphasizes the inclusion of the limits while < would hint for the exclusion.

My previous attempt was

$inRange = ($x > $a == $x < $b) || $x == $a || $x == $b; (inclusive)
$inRange = ($x > $a == $x < $b); (exclusive)

which seemed less elegant, but the exclusive version might be faster.

Cheers, Heiko

Replies are listed 'Best First'.
Re^2: Range check with unordered limits
by syphilis (Archbishop) on Jul 13, 2022 at 08:23 UTC
    $inRange = ($x > $a == $x < $b) || $x == $a || $x == $b; (inclusive)

    Two thoughts:

    1) Isn't that equivalent to:
    $inRange = ($x >= $a == $x <= $b);
    Update: Yes, it isn't ;-) Thanks LanX.

    2) Is it safe to assume that ($x > $a) and ($x < $b) will return the same value whenever they are true ?
    It probably is ... but the docs merely say that they will return a true value, with no explicit guarantee that the magnitude of the "true value" will always be the same.

    Cheers,
    Rob
      > 1) Isn't that equivalent to:

      No, your version fails the limits when they are swapped

      Here a generalized test suite for everyones experiments:

      use v5.12; use warnings; our ($a,$b,$x); for my $lim ( [2,4], [4,2] ) { ($a,$b) = @$lim; for $x (1..5) { my @r = (&s0,&s1); unless ( $r[0] == $r[1] ) { say "fails for $x in [@$lim] with <$r[0]>,<$r[1]>"; } } } sub s0 { ($x > $a == $x < $b) || $x == $a || $x == $b; } sub s1 { $x >= $a == $x <= $b; }

      c:/tmp/pm/clever_intervall.pl fails for 2 in [4 2] with <1>,<> fails for 4 in [4 2] with <1>,<>

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re^2: Range check with unordered limits
by LanX (Saint) on Jul 13, 2022 at 09:19 UTC
    > I agree with ... LanX that ... is the most canonical form

    Did I say this?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      IMHO for a multiplying approach that's the best check, even for the <=> version.
      So not verbally, but you preferred this version, I understood (as I do now).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-03-28 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found