java - strange behaviour of comparing two signed integers in a for loop -
i trying solve problem stated follows: given set segments, , set of points, calculate how many segments contain each point.
the problem have encounterd when have count how many times point contained segment. when have input, inner loop, increments correctly counter of each point, weather when have data set, comparing 0 negative number , non-negative number takes place, behaves strangely.
the following script created locate problem facing, , not represent actual implementation.
the test cases give outputs follow:
case 1:
string debug = "test case 1: \n "; debug += " \n - 2 segments coordinates [0, 5] , [7, 10]."; debug += " \n - 3 points @ coordinates 1, 6, , 11."; int [] starts = new int[]{0, 7}; int [] ends = new int[]{5, 10}; int [] points = new int[]{1, 6, 11}; debug += "\n \n calculating coverage of points: "; ( int i=0; i<starts.length; i++) { (int j=0; j<points.length && ( starts[i] <= points[j] && points[j] <= ends[i]); j++) { debug += " \n * point coordinate " + points[j] + ", between " + starts[i] + " , " + ends[i]; } } debug += "\n \n finished calculation!"; int start = 0, point = 1, end = 5; debug += "\n \n custom check 1st point: "; debug += "\n - (" + start + " <= " + point + " , " + point + " <= " + end + ")? " + ( start <= point && point <= end ); system.out.println(debug);
output:
test case 1:
- 2 segments coordinates [0, 5] , [7, 10].
3 points @ coordinates 1, 6, , 11.
calculating coverage of points:
point coordinate 1, between 0 , 5
finished calculation!
custom check 1st point:
- is (0 <= 1 , 1 <= 5)? true
case 2:
string debug = "test case 2: \n "; debug += " \n - 1 segment coordinates [-10, 10]."; debug += " \n - 3 points @ coordinates -100, 100, , 10."; int [] starts = new int[]{-10}; int [] ends = new int[]{10}; int [] points = new int[]{-100, 100, 0}; debug += "\n \n calculating coverage of points: "; ( int i=0; i<starts.length; i++) { (int j=0; j<points.length && ( starts[i] <= points[j] && points[j] <= ends[i]); j++) { debug += " \n * point coordinate " + points[j] + ", between " + starts[i] + " , " + ends[i]; } } debug += "\n \n finished calculation!"; int start = -10, point = 0, end = 10; debug += "\n \n custom check: "; debug += "\n - (" + start + " <= " + point + " , " + point + " <= " + end + ")? " + ( start <= point && point <= end ); system.out.println(debug);
output:
test case 2:
- 1 segment coordinates [-10, 10].
3 points @ coordinates -100, 100, , 10.
calculating coverage of points:
finished calculation!
custom check:
- is (-10 <= 0 , 0 <= 10)? true
as can see, condition @ inner loop somehow not calculating appropriately case of point coordinate 0, relative segment [-10, 10].
thanks in advance, endrit.
your problem for
loop condition:
for (int j=0; j<points.length && ( starts[i] <= points[j] && points[j] <= ends[i]); j++) {
as encounter point[j]
not lie between starts[i]
, ends[i]
loop terminate , won't check subsequent points.
instead, should separate loop condition intersection condition. in pseudocode:
for every (start, end) pair: every point: if point intersects (start, end): increment counter otherwise continue next point
does make sense?
Comments
Post a Comment