Change the implementation of ArrayUtils.range to include the stop value if it is a multiple of step.
This commit is contained in:
parent
ead406e852
commit
43b269c0f4
@ -6,16 +6,11 @@ public class ArrayUtils {
|
||||
|
||||
/**
|
||||
* Returns a double array with values from start to end with given step.
|
||||
* Starts exactly at start and stops at the step before stop is reached.
|
||||
* Starts exactly at start and stops at the multiple of step <= stop.
|
||||
*/
|
||||
public static double[] range(double start, double stop, double step){
|
||||
|
||||
int size = 0 ;
|
||||
if ( stop <= start ) {
|
||||
size = 0;
|
||||
} else {
|
||||
size = (int) Math.ceil(((stop - start) / step));
|
||||
}
|
||||
int size = (int) Math.floor(((stop - start) / step)) + 1;
|
||||
|
||||
//System.out.println("Range from "+start+" to "+stop+" step "+step+" has length "+size);
|
||||
|
||||
|
@ -80,6 +80,13 @@ public class ArrayUtilsTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRante0() {
|
||||
double[] ary = ArrayUtils.range(0., 0., 1.0);
|
||||
assertEquals(1, ary.length);
|
||||
assertEquals( 0.0, ary[0], 0.0 );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRange1() {
|
||||
double[] ary = ArrayUtils.range(0.0, 0.5, 1.0);
|
||||
@ -90,9 +97,10 @@ public class ArrayUtilsTest {
|
||||
@Test
|
||||
public void testRange2() {
|
||||
double[] ary = ArrayUtils.range(0.0, 1.0, 0.5);
|
||||
assertEquals(2, ary.length);
|
||||
assertEquals(3, ary.length);
|
||||
assertEquals( 0.0, ary[0], 0.0 );
|
||||
assertEquals( 0.5, ary[1], 0.0 );
|
||||
assertEquals( 1.0, ary[2], 0.0 );
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -107,9 +115,9 @@ public class ArrayUtilsTest {
|
||||
@Test
|
||||
public void testRange4() {
|
||||
double[] ary = ArrayUtils.range(0.0,10.0, 0.5);
|
||||
assertEquals( 20, ary.length );
|
||||
assertEquals( 21, ary.length );
|
||||
int i =0;
|
||||
for( double d = 0.0; d < 10.0; d+=0.5){
|
||||
for( double d = 0.0; d < 10.2; d+=0.5){
|
||||
assertEquals(d,ary[i++],0.0);
|
||||
}
|
||||
assertEquals( i, ary.length );
|
||||
|
Loading…
x
Reference in New Issue
Block a user