简体   繁体   English

你如何找到一个数字是否在Java的范围内? Math.abs(num1-num2) <= inRange 的问题

[英]How do you find if a number is within a range in Java? Problems with Math.abs(num1-num2) <= inRange

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.其中 inRange 是您试图确定它是否在 num2 和 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.我试图找出 -5 是否介于 -25 和 25 之间。即使答案是正确的,这个等式也是错误的,-5 介于 -25 和 25 之间。

Please clarify for me!请为我澄清!

I don't see any reason to use Math.abs at all.我根本看不出有任何理由使用Math.abs 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.事实上, Math.abs()方法似乎完全被破坏了 - 我强烈怀疑你误解了它作为解决方案提出的问题。

Just do:做就是了:

bool isInRange = Math.min(num1,num2) <= inRange 
                && Math.max(num1,num2) >= inRange;

Your current approach just checks number ranges.您当前的方法只是检查数字范围。 in fact smallest and largest number distance.实际上最小和最大数距离。

For bonus points, there is a new Range class (used with helper class Ranges ) introduced in Guava 10.x:对于奖励积分,Guava 10.x 中引入了一个新的Range类(与辅助类Ranges 一起使用):

import com.google.common.collect.Range;
import com.google.common.collect.Ranges;

public class RangeTest {

    Range<Integer> range = Ranges.closed(-25, +25);

    public boolean rangeTest(Integer candidate) {
        return range.contains(candidate);
    }

}


public class TestMain {
    static RangeTest rangeTest = new RangeTest();

    public static void doTest(Integer candidate) {
        System.out.println(candidate + " in -25..+25: "
                + rangeTest.rangeTest(candidate));
    }

    public static void main(String[] args) {
        doTest(-26);
        doTest(-25);
        doTest(-24);
        doTest(-1);
        doTest(-0);
        doTest(+1);
        doTest(+24);
        doTest(+25);
        doTest(+26);
    }

}

Output:输出:

-26 in -25..+25: false -26 in -25..+25: 假
-25 in -25..+25: true -25 in -25..+25: 真
-24 in -25..+25: true -24 in -25..+25: true
-1 in -25..+25: true -1 in -25..+25: 真
0 in -25..+25: true -25..+25 中的 0:真
1 in -25..+25: true -25..+25 中的 1:真
24 in -25..+25: true 24 in -25..+25: true
25 in -25..+25: true 25 in -25..+25: true
26 in -25..+25: false 26 in -25..+25: 假

The Range class supports open and closed ranges, ranges from -INF to +INF, and all sorts of range-related operations like membership, intersection, and span. Range 类支持开和闭范围,范围从 -INF 到 +INF,以及各种与范围相关的操作,如成员资​​格、交集和跨度。

下面的表达式将检查x是否在ab之间:

Math.abs(x - a) + Math.abs(b - x) == Math.abs(b - a)

Old question, however the Math.abs method can be clearer depending what you're working on, and is still worth showing:老问题,但是 Math.abs 方法可以根据您正在处理的内容更清晰,并且仍然值得展示:

int x = 5;
int bounds = 25;
if(Math.abs(x) <= bounds) {
    //run if x is anywhere between -25 and 25
}

kotlin has .. operator kotlin 有 .. 运算符

if (center.x in 0..MaxX) {
                   //Do stuff
}
if (center.y !in 0..MaxY) {
                    //Do stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM