J
Jeff Higgins
Hi,
I'm trying to find a general solution for the problem.
Given an arbitrary:
Double increment,
Double rangeMinimum,
Double rangeMaximum,
Integer scale.
Output:
a series of Doubles that are increments in the range,
rangeMinimum through rangeMaximum with the given scale.
I've tried several approaches, but so far they have all
failed to give the desired output for one or more test cases.
I'm hoping someone can point me in the right direction.
I hope I'm explaining this clearly but for instance the
following code fails to give my desired output for the case:
Double increment = 1.0/3.0;
Double rangeMin = -2.67;
Double rangeMax = 2.31;
Double scale = 1.0;
Gives:
[-2.7, -2.4, -2.1, -1.7, -1.4, -1.1, -0.7,
-0.4, -0.1, 0.3, 0.6, 0.9, 1.3, 1.6, 1.9]
Rather than the desired:
[-2.6, -2.3, -2.0, -1.6, -1.3, -1.0, -0.6,
-0.3, 0.0, 0.3, 0.6, 1.0, 1.3, 1.6, 2.0]
***********************************************
These cases give the desired output.
for case:
Double increment = 10.0;
Double rangeMin = -100.0;
Double rangeMax = 100.0;
Double scale = 0;
Gives the desired;
[-110,-100,-90,-80,-70,-60,-50,-40,-30,-20,
-10,0,10,20,30,40,50,60,70,80,90,100]
for case:
Double increment = .25;
Double rangeMin = -2.67;
Double rangeMax = 2.31;
Double scale = 3.0;
Gives the desired;
[-2.500,-2.250, -2.000, -1.750, -1.500, -1.250, -1.000,
-0.750, -0.500, -0.250, 0.000, 0.250, 0.500,
0.750, 1.000, 1.250, 1.500, 1.750, 2.000, 2.250]
Thanks for any help.
Jeff Higgins
import java.math.BigDecimal;
public class graduate {
public static void main(String[] args) {
Double increment = 10.0;
Double rangeMin = -100.0;
Double rangeMax = 100.0;
int scale = 1;
// find a starting point ************************************************
Double remainder = Math.abs(rangeMin - rangeMin.intValue());
boolean test = true;
Double testDouble = remainder;
int count = 0;
while(test){
testDouble = testDouble - increment;
test = (testDouble - increment) >= 0;
++count;
}
BigDecimal start = new BigDecimal(rangeMin.intValue() - increment *
count);
// ***********************************************************************
BigDecimal inc = new BigDecimal(increment);
test = true;
BigDecimal loop = start.setScale(scale + 1,BigDecimal.ROUND_HALF_EVEN);
while(test){
System.out.println(loop.setScale(scale,BigDecimal.ROUND_CEILING));
loop = loop.add(inc);
test = loop.doubleValue() <= 0;
}
test = true;
BigDecimal loop2 = loop.setScale(scale + 1,BigDecimal.ROUND_HALF_EVEN);
while(test){
System.out.println(loop2.setScale(scale,BigDecimal.ROUND_FLOOR));
loop2 = loop2.add(inc);
test = loop2.doubleValue() <= rangeMax;
}
}
}
I'm trying to find a general solution for the problem.
Given an arbitrary:
Double increment,
Double rangeMinimum,
Double rangeMaximum,
Integer scale.
Output:
a series of Doubles that are increments in the range,
rangeMinimum through rangeMaximum with the given scale.
I've tried several approaches, but so far they have all
failed to give the desired output for one or more test cases.
I'm hoping someone can point me in the right direction.
I hope I'm explaining this clearly but for instance the
following code fails to give my desired output for the case:
Double increment = 1.0/3.0;
Double rangeMin = -2.67;
Double rangeMax = 2.31;
Double scale = 1.0;
Gives:
[-2.7, -2.4, -2.1, -1.7, -1.4, -1.1, -0.7,
-0.4, -0.1, 0.3, 0.6, 0.9, 1.3, 1.6, 1.9]
Rather than the desired:
[-2.6, -2.3, -2.0, -1.6, -1.3, -1.0, -0.6,
-0.3, 0.0, 0.3, 0.6, 1.0, 1.3, 1.6, 2.0]
***********************************************
These cases give the desired output.
for case:
Double increment = 10.0;
Double rangeMin = -100.0;
Double rangeMax = 100.0;
Double scale = 0;
Gives the desired;
[-110,-100,-90,-80,-70,-60,-50,-40,-30,-20,
-10,0,10,20,30,40,50,60,70,80,90,100]
for case:
Double increment = .25;
Double rangeMin = -2.67;
Double rangeMax = 2.31;
Double scale = 3.0;
Gives the desired;
[-2.500,-2.250, -2.000, -1.750, -1.500, -1.250, -1.000,
-0.750, -0.500, -0.250, 0.000, 0.250, 0.500,
0.750, 1.000, 1.250, 1.500, 1.750, 2.000, 2.250]
Thanks for any help.
Jeff Higgins
import java.math.BigDecimal;
public class graduate {
public static void main(String[] args) {
Double increment = 10.0;
Double rangeMin = -100.0;
Double rangeMax = 100.0;
int scale = 1;
// find a starting point ************************************************
Double remainder = Math.abs(rangeMin - rangeMin.intValue());
boolean test = true;
Double testDouble = remainder;
int count = 0;
while(test){
testDouble = testDouble - increment;
test = (testDouble - increment) >= 0;
++count;
}
BigDecimal start = new BigDecimal(rangeMin.intValue() - increment *
count);
// ***********************************************************************
BigDecimal inc = new BigDecimal(increment);
test = true;
BigDecimal loop = start.setScale(scale + 1,BigDecimal.ROUND_HALF_EVEN);
while(test){
System.out.println(loop.setScale(scale,BigDecimal.ROUND_CEILING));
loop = loop.add(inc);
test = loop.doubleValue() <= 0;
}
test = true;
BigDecimal loop2 = loop.setScale(scale + 1,BigDecimal.ROUND_HALF_EVEN);
while(test){
System.out.println(loop2.setScale(scale,BigDecimal.ROUND_FLOOR));
loop2 = loop2.add(inc);
test = loop2.doubleValue() <= rangeMax;
}
}
}