Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

need help counting

Thread view: 
Jeff Higgins - 06 Jul 2006 19:44 GMT
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;
 }
}
}
jhr - 06 Jul 2006 21:56 GMT
> Hi,
> I'm trying to find a general solution for the problem.
[quoted text clipped - 91 lines]
> }
> }

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
Jeff Higgins - 06 Jul 2006 22:23 GMT
>>   // find a starting point
>> ************************************************
[quoted text clipped - 15 lines]
> 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;
 }
}
}
Jeff Higgins - 06 Jul 2006 22:34 GMT
Jeff Higgins
>> 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

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

Thanks
Jeff
Jeff Higgins - 06 Jul 2006 22:38 GMT
> Should be:

> import java.math.BigDecimal;
>
[quoted text clipped - 28 lines]
>  }
>  test = true;

   StringBuilder s = new StringBuilder(".");

>  for(int i = 0; i < scale; ++i){
>    s.append("0");
[quoted text clipped - 9 lines]
> }
> }
Fred Kleinschmidt - 06 Jul 2006 22:23 GMT
> Hi,
> I'm trying to find a general solution for the problem.
[quoted text clipped - 78 lines]
>  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)

>  while(test){
>    System.out.println(loop.setScale(scale,BigDecimal.ROUND_CEILING));
[quoted text clipped - 11 lines]
> }
>
Signature

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Jeff Higgins - 06 Jul 2006 23:04 GMT
>> Hi,
>> I'm trying to find a general solution for the problem.
[quoted text clipped - 84 lines]
> 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

>>  while(test){
>>    System.out.println(loop.setScale(scale,BigDecimal.ROUND_CEILING));
[quoted text clipped - 10 lines]
>> }
>> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.