Results 1 to 6 of 6
  1. #1
    lipkerson is offline Member
    Join Date
    May 2010
    Location
    Poland-Silesia
    Posts
    17
    Rep Power
    0

    Default BigDecimal pow function

    Hi

    I'am looking for a method that operate on BigDecimals. I nedd to do x^y where x and y are BigDecimals.
    I found a commercial library SuanShu but I prefer free solution.

    There is

    but exponent must be from range and with decimal part = 0. Otherwise it throws AritmeticException.

    with regards

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

    Default Re: BigDecimal pow function

    Suppose you want to calculate a^b where both a and b are BigDecimals; suppose b has n fractional digits, so 10^n*b is an int or BigInteger.

    So, a^b == a^(10^n*b)/a^(10^n); this reduces the problem to taking the power of a raised to a BigInteger or an int (twice); raising a^x where x is a BigInteger can be reduced to:

    a^x == a^(x/2)*a^(x/2)*x iff x is odd;
    a^x == a^(x/2)*a^(x/2) iff x is even;
    a^x == a iff x is 1;
    a^x == 1 iff x is 0
    a^x can be calculated with BigDecimal.pow( ... ) iff x <= Integer.MAX_VALUE.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,736
    Rep Power
    7

    Default Re: BigDecimal pow function

    Quote Originally Posted by JosAH View Post
    Suppose you want to calculate a^b where both a and b are BigDecimals; suppose b has n fractional digits, so 10^n*b is an int or BigInteger.

    So, a^b == a^(10^n*b)/a^(10^n)
    I don't believe this is true. Let a = 2.0, b = 1.2. Then n = 1
    (a^12)/(a^10) = a^2 = 4. But 2^1.2 is approx 2.297.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: BigDecimal pow function

    Quote Originally Posted by jim829 View Post
    I don't believe this is true. Let a = 2.0, b = 1.2. Then n = 1
    (a^12)/(a^10) = a^2 = 4. But 2^1.2 is approx 2.297.
    Of course it isn't true; some moron must've doodled it down while he was still asleep (he probably hadn't had his espresso yet); the last part of all that nonsense is true but trivial; I didn't have my esprsso yet either; I'll probably get back to this later. Maybe a bit of fiddling with the BigDecimal.setScale( ... ) method can do the trick ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,736
    Rep Power
    7

    Default Re: BigDecimal pow function

    Well, I've worked on this some but I can't seem to figure it out. Somewhere, somehow I keep getting stuck with having to take the 10^n root of the result. This is because a^(10^n*b) == (a^b)^10^n. In any event, I usually end up with some decimal as part of the exponent so I am back to square one.

    Regards,
    Jim

    P.S. Savor the Espresso.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: BigDecimal pow function

    Quote Originally Posted by jim829 View Post
    Well, I've worked on this some but I can't seem to figure it out. Somewhere, somehow I keep getting stuck with having to take the 10^n root of the result. This is because a^(10^n*b) == (a^b)^10^n. In any event, I usually end up with some decimal as part of the exponent so I am back to square one.

    Regards,
    Jim

    P.S. Savor the Espresso.
    I have no results either; I think you have to go the log/exp way, i.e. a^b= exp(log(a^b)) == exp(b*log(a)) and hopefully some of the calculations can be cut out ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. BigDecimal
    By tarka in forum New To Java
    Replies: 4
    Last Post: 11-12-2011, 09:22 PM
  2. Divide bigdecimal again
    By ellhar in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 11:19 AM
  3. BigDecimal and BigInteger
    By jon80 in forum New To Java
    Replies: 8
    Last Post: 11-28-2010, 12:15 PM
  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

Tags for this Thread

Posting Permissions

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