
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

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:
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));
}
kind regards,
Jos

Re: BigDecimal
Thanks Jos, that is basically what I have done whilst waiting for an answer (although not as elegantly). :o:
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

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:
Code:
for (int i = someValue; i > 0; i) {
sum *= i;
}
The problem is that sum needs to be a BigDecimal as its too large for a long. My attempts thus far have been to make i a BigDecimal, then I have setup a new BigDecimal with a value of 0 and used the compareTo() to replace the i > 0; I then needed to setup ANOTHER BigDecimal with a value of 1 and use the subtract() to replace the i. I have now come a bit unstuck with the sum *= i part and how to make that work with BD.
I tried setting up yet another BD called sum and then doing this:
Code:
sum = bd1.multiply(bd1.multiply(i));
It all sems like alot of BD and not only that but it doesn't actually work!

Re: BigDecimal
I'd rather name that variable 'product' instead of 'sum'; note that the BigDecimal class has a few useful constants (ONE or ZERO).
kind regards,
Jos