Results 1 to 6 of 6
Thread: BigDecimal pow function
 07022013, 09:03 AM #1Member
 Join Date
 May 2010
 Location
 PolandSilesia
 Posts
 17
 Rep Power
 0
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
 07022013, 11:10 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,292
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 07042013, 04:41 AM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,303
 Rep Power
 9
Re: BigDecimal pow function
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 07042013, 08:51 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,292
 Blog Entries
 7
 Rep Power
 24
Re: BigDecimal pow function
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 07042013, 12:50 PM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,303
 Rep Power
 9
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 Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 07042013, 03:30 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,292
 Blog Entries
 7
 Rep Power
 24
Re: BigDecimal pow function
The only person who got everything done by Friday was Robinson Crusoe.
Similar Threads

BigDecimal
By tarka in forum New To JavaReplies: 4Last Post: 11122011, 09:22 PM 
Divide bigdecimal again
By ellhar in forum New To JavaReplies: 3Last Post: 03232011, 11:19 AM 
BigDecimal and BigInteger
By jon80 in forum New To JavaReplies: 8Last Post: 11282010, 12:15 PM 
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