Results 1 to 5 of 5

Thread: BigDecimal

  1. #1
    tarka is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

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

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    tarka is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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

  4. #4
    tarka is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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;
    }
    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:

    Java Code:
    sum = bd1.multiply(bd1.multiply(i));
    It all sems like alot of BD and not only that but it doesn't actually work!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. More sensible BigDecimal toString()?
    By morello in forum Advanced Java
    Replies: 3
    Last Post: 09-29-2011, 04:08 PM
  2. Negating a BigDecimal
    By ryanmk54 in forum New To Java
    Replies: 5
    Last Post: 05-16-2011, 06:23 PM
  3. Divide bigdecimal again
    By ellhar in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 10:19 AM
  4. Using BigDecimal
    By doymand in forum New To Java
    Replies: 2
    Last Post: 10-30-2010, 09:25 AM
  5. BigDecimal question
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •