Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > calculating the a bigdecimal to the power of double

Reply
Thread Tools

calculating the a bigdecimal to the power of double

 
 
manzur
Guest
Posts: n/a
 
      04-20-2006

Patricia Shanahan wrote:
> manzur wrote:
> > I have a bigdecimal and a double value,say
> >
> > Bigdecimal bd = 599999.45;
> > double d= 5.9;
> >
> > i want to do 599999.45^5.9
> >
> > Note:I dont want to convert bigdecimal to double
> >
> > thanks in advance.
> >

>
> As already said, non-integer powers in BigDecimal would be a significant
> project.
>
> Maybe there is a less drastic solution to your problem. Could you
> explain why you don't want to do the power calculation in double? That
> may give someone ideas for alternatives.
>
> Patricia



iam writing code for some banking applications which demand accuracy
in money
values.For money values iam using Bigdecimal. If i convert my
bigdecimal to double i fear of inaccuracy(In cases where i deal with
huge values greater than double)

 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-20-2006
manzur wrote:
> Patricia Shanahan wrote:
>
>>manzur wrote:
>>
>>>I have a bigdecimal and a double value,say
>>>
>>>Bigdecimal bd = 599999.45;
>>>double d= 5.9;
>>>
>>>i want to do 599999.45^5.9
>>>
>>>Note:I dont want to convert bigdecimal to double
>>>
>>>thanks in advance.
>>>

>>
>>As already said, non-integer powers in BigDecimal would be a significant
>>project.
>>
>>Maybe there is a less drastic solution to your problem. Could you
>>explain why you don't want to do the power calculation in double? That
>>may give someone ideas for alternatives.
>>
>>Patricia

>
>
>
> iam writing code for some banking applications which demand accuracy
> in money
> values.For money values iam using Bigdecimal. If i convert my
> bigdecimal to double i fear of inaccuracy(In cases where i deal with
> huge values greater than double)
>


You won't be dealing with values greater than double, because the
maximum double is bigger than 10^308. That is more than the probable
number of atoms in the observable universe.

However, it is entirely possible that you could get a wrong answer for
the least significant digit on large sums of money. Double is just about
precise enough to get the cents digit right on the US national debt in
dollars. If that is good enough, one solution might be to use double for
the exponentiation, but immediately convert back to BigDecimal. That
would avoid accumulating rounding errors during routine addition and
subtraction.

I'm a bit surprised by the use of exponentiation in this precise an
environment. For example, I would have expected compound interest to be
done a compounding period at a time, with the interest for each period
rounded according to fixed rules, then added to the balance.

Patricia
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-20-2006
On 19 Apr 2006 21:58:45 -0700, "manzur" <> wrote,
quoted or indirectly quoted someone who said :

> iam writing code for some banking applications which demand accuracy
>in money
>values.For money values iam using Bigdecimal. If i convert my
>bigdecimal to double i fear of inaccuracy(In cases where i deal with
>huge values greater than double)


Hmm. What you might do is get a good initial approximation with
Math.pow on double. Then use a Newton-Raphson to home in on a very
accurate value.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      04-20-2006
Patricia Shanahan wrote:

> I'm a bit surprised by the use of exponentiation in this precise an
> environment. For example, I would have expected compound interest to be
> done a compounding period at a time, with the interest for each period
> rounded according to fixed rules, then added to the balance.


Another possibility is that "perfect" precision is not required for /this
particular/ calculation. For instance in the UK any offer of a loan must be
accompanied by (besides the exact statement of interest, etc) an indicator
figure which is supposed to make it easier for people to compare offers with
different conditions. I forget the thing's name, and the rules for computing
it are complicated, but the point is that floating-point accuracy is entirely
adequate, even though it's a financial calculation.

-- chris


 
Reply With Quote
 
andyt andyt is offline
Junior Member
Join Date: Dec 2010
Posts: 1
 
      12-16-2010
Hi,

I've just released a package with a method in for this, but for some reason the forum won't let me post a link until I have posted 50 messages, so I've mangled the URL. Hopefully you can decode it and find what you want:

http_www_geog_leeds_ac_uk/people/a.turner/src/andyt/java/generic/

I have released under LGPL, but could release with a different license if anyone has a problem with that.

Best wishes,

Andy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BigDecimal and double arithmetic problems Ramon Java 12 11-03-2008 11:58 AM
BigDecimal(String) vs. BigDecimal(double) [Floating-point arithmetics] Stanimir Stamenkov Java 4 07-18-2008 10:49 AM
BigDecimal to power of BigDecimal czarnysfetr@gmail.com Java 3 02-05-2007 01:49 PM
Calculating hashcode in double hashing Mikke Java 7 05-24-2004 09:12 PM
Problem with Double to BigDecimal Conversion Forrest Hump Java 4 08-22-2003 03:53 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57