in reply to Range check with unordered limits
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 | |
by LanX (Saint) on Jul 13, 2022 at 09:17 UTC | |
Re^2: Range check with unordered limits
by LanX (Saint) on Jul 13, 2022 at 09:19 UTC | |
by hexcoder (Curate) on Jul 13, 2022 at 09:32 UTC | |
by LanX (Saint) on Jul 13, 2022 at 12:09 UTC | |
Re^2: Range check with unordered limits
by hexcoder (Curate) on Jun 08, 2024 at 09:36 UTC |