in reply to Re^2: Range check with unordered limits
in thread Range check with unordered limits
IMHO for a multiplying approach that's the best check, even for the <=> version.
Simply changing to ... < 0 would check for the open interval excluding the endpoints. And ... == 0 for endpoints only.
But half-open intervals like [$a,$b[ can't be covered with this approach.
So I'd rather stick with
and swapping can be made non-destructive by localizing it to a scope.
C:\tmp\e>perl ($a,$b) = (7,3); my $x=5; my $inside = do { local ($a,$b) = ($b,$a) if $b < $a; $a <= $x < $b ; }; print "$x in [$a, $b]? $inside"; __END__ 5 in [7, 3]? 1 C:\tmp\e>
(replace local with my for private vars)
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
|
---|