need help counting

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;
}
}
}
 
J

jhr

Jeff said:
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;
}
}
}

explain how if the minRange is -2.67, the increment is .25, the scale
is 3, then the first desired element is -2.500 and not -2.420.
-2.67+.25 = -2.42
 
F

Fred Kleinschmidt

Jeff Higgins said:
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);

This statement causes 'start' to be rounded to the nearest neighbor with
scale=2.
For your 'filure' case, 'start' is -2.66666666..., so the result is -2.67
(nearer to 'start' than -2.66 is)
 
J

Jeff Higgins

jhr said:
explain how if the minRange is -2.67, the increment is .25, the scale
is 3, then the first desired element is -2.500 and not -2.420.
-2.67+.25 = -2.42

I wish my series to begin on the most negative value of the range that
is evenly divisible by the increment. That is why I've included the
(probably clumsy)find starting point code above to find a starting point.

Since my last post I've revised the code to the following (probably clumsy).
Which gives the desired results for all the posted examples.

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;
for(int i = 0; i < scale; ++i){
s.append("0");
}
s.append("1");

BigDecimal loop2 = new BigDecimal(s.toString());
while(test){
System.out.println(loop2.setScale(scale,BigDecimal.ROUND_FLOOR));
loop2 = loop2.add(inc);
test = loop2.doubleValue() <= rangeMax;
}
}
}
 
J

Jeff Higgins

Jeff Higginsalso, Double scale = 3.0; is an unfortunate cut and paste typo.
Should be Double scale = 2.0;

Thanks
Jeff
 
J

Jeff Higgins

Jeff Higgins wrote
Should be:
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;

StringBuilder s = new StringBuilder(".");
 
J

Jeff Higgins

Fred Kleinschmidt said:
Jeff Higgins said:
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);

This statement causes 'start' to be rounded to the nearest neighbor with
scale=2.
For your 'filure' case, 'start' is -2.66666666..., so the result is -2.67
(nearer to 'start' than -2.66 is)

yes, I just realized my starting point is no good for increment > 1.
working on it now.
Thanks.
Jeff Higgins
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top