in reply to Re: Range check with unordered limits
in thread Range check with unordered limits

$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

Replies are listed 'Best First'.
Re^3: Range check with unordered limits
by LanX (Saint) on Jul 13, 2022 at 09:17 UTC
    > 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