I've seen in another question that the solution to finding if your number is in a range was,
Math.abs(num1-num2) <= inRange
Where inRange is the number you are trying to figure out if it is in range between num2 and num1.
Where this formula breaks for me is when I insert these numbers.
Math.abs(25-(-25)) <= -5
I'm trying to find if -5 is in between -25 and 25. This equation is false even though the answer is true, -5 falls between -25 and 25.
Please clarify for me!
解决方案
I don't see any reason to use Math.abs at all. I'd use:
if (lowerBound <= value && value < upperBound)
or
if (lowerBound <= value && value <= upperBound)
if you want the upper bound to be inclusive too.
Indeed, the Math.abs() approach seems entirely broken - I strongly suspect that you misunderstood the question where it was posed as a solution.