Results 1 to 5 of 5
Thread: BigDecimal
 11122011, 03:06 PM #1Member
 Join Date
 Nov 2011
 Posts
 3
 Rep Power
 0
BigDecimal
This is a two part question. Firstly is there a max limit that BigDecimal can display or is it just a case that I'm running out of memory? I need to calculate a number that has a little under 2 million digits in it and at the moment I can only seem to get an outbut for around Math.pwr(2, 1500). Regardless of whether its memory or a limitation by BigDecimal how would you go about generating such a number.
The second part of the question requires that I break down the individual digits that make up the BigDecimal so that I can work with them individually. I know that there are lots of ways to do this, namely converting to a string, or an array etc. But I was wondering if there was a way of working directly with the individual values of BigDecimal (and Big Int for that matter). I have looked at the API but I couldn't see anything that was immedietly obviouse.
Thanks
 11122011, 05:01 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
Re: BigDecimal
There is no imposed limit to a BigDecimal number, except for the amount of available RAM in your computer. If you know that your result is a power of two, you can do the power operator yourself:
Java Code:BigDecimal powOf2(int expo) { if (expo == 0) return BigDecimal.ONE; in (expo == 1) return new BigDecimal(2); BigDecimal half= powOf2(expo/2); if (expo&1 == 0) return half.times(half); else return half.times(half).times(new BigDecimal(2)); }
JosThe only person who got everything done by Friday was Robinson Crusoe.
 11122011, 06:21 PM #3Member
 Join Date
 Nov 2011
 Posts
 3
 Rep Power
 0
Re: BigDecimal
Thanks Jos, that is basically what I have done whilst waiting for an answer (although not as elegantly).
Any chance you can cast some light on the handling of individual numbers. It seems very messy having to change the type so many times just to work with the numbers. I can't think of another way of doint it other than going from:
number > String (or array etc.) > number
Is there not some kind of method similar to charAt() that can be used to isolate individual values?
intAt(), can't find it in the API ???
cheers
 11122011, 07:39 PM #4Member
 Join Date
 Nov 2011
 Posts
 3
 Rep Power
 0
Re: BigDecimal
Now that I've looked into the problem deeper I have come up against another question. I need to run a for loop as follows:
Java Code:for (int i = someValue; i > 0; i) { sum *= i; }
I tried setting up yet another BD called sum and then doing this:
Java Code:sum = bd1.multiply(bd1.multiply(i));
 11122011, 09:22 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
Similar Threads

More sensible BigDecimal toString()?
By morello in forum Advanced JavaReplies: 3Last Post: 09292011, 04:08 PM 
Negating a BigDecimal
By ryanmk54 in forum New To JavaReplies: 5Last Post: 05162011, 06:23 PM 
Divide bigdecimal again
By ellhar in forum New To JavaReplies: 3Last Post: 03232011, 11:19 AM 
Using BigDecimal
By doymand in forum New To JavaReplies: 2Last Post: 10302010, 09:25 AM 
BigDecimal question
By orchid in forum New To JavaReplies: 2Last Post: 08122008, 01:44 PM
Bookmarks