How to check if a number is within a range without $<$, $>$?

593 Views Asked by At

I know we can simply check if $x$ is within $(\min,\max)$ by the following function:

if (x > min && x < max) then x lies inside of the range.

Can I have a function which uses neither $>$ nor $<$ operators to do this? Any other operator like $==,-,+,*,/,...$ is allowed.

It's all about changing traditions and increasing math skills. (fun with math)

thanks.

1

There are 1 best solutions below

3
On BEST ANSWER

I guess you could use an implicit True or False.

Here's an example for positive numbers.

if x//min and not x//max.

if x > min, then x//min is nonzero, and if x < max, then x//max is zero.

There are some languages where zero is treated as a False value.


Let's do a modification to this for min and max which may be nonpositive.

All we need to do is map $min$ to $1$ and map max to $max-min+1$.

We can't map min to $0$ because division fails there.

$x$ gets mapped to $x-min+1$.

So we would test $(x-min+1)//1$ and not $(x-min+1)//(max-min+1)$