Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

calculating the a bigdecimal to the power of double

Thread view: 
manzur - 19 Apr 2006 17:15 GMT
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.
Thomas Hawtin - 19 Apr 2006 17:26 GMT
> I have a bigdecimal  and a double value,say
>
[quoted text clipped - 4 lines]
>
> Note:I dont want to convert bigdecimal to double

Raising a decimal to the power of 5.9 isn't going to result in a number
expressible as a decimal. If you want more accuracy than provided by
double, I guess you could use a series expansion.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Danno - 19 Apr 2006 17:59 GMT
I don't think you can xor floating points just integers.
Roedy Green - 19 Apr 2006 18:54 GMT
>I don't think you can xor floating points just integers.

He means ^ to represent power.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Danno - 20 Apr 2006 02:41 GMT
There must've been some invisible thing on there. ;) hehe
Danno - 20 Apr 2006 03:28 GMT
Ah, the title!

sh.t, I never look there. ;)
Eric Sosman - 19 Apr 2006 18:36 GMT
manzur wrote On 04/19/06 12:15,:
> I have a bigdecimal  and a double value,say
>
[quoted text clipped - 4 lines]
>
> Note:I dont want to convert bigdecimal to double

   Suggestion #1: Convert it to double anyhow, despite
your dislike of the approach.

   Suggestion #2: Re-examine what you're trying to do.
Is it really a good idea to use a BigDecimal here?

   Suggestion #3: Get out your Numerical Methods books
and start writing implementations for transcendental
functions of BigDecimal values, using add() and multiply()
and the other methods of BigDecimal to perform the
underlying arithmetic.  (Warning: This will probably not
be a project, but a PROJECT ...)

Signature

Eric.Sosman@sun.com

Roedy Green - 19 Apr 2006 18:53 GMT
>Bigdecimal bd = 599999.45;
>double d= 5.9;

That won't compile for two reasons.  

1. you spelled BigDecimal incorrectly

2. BigDecimal is not a primitive. You need to use a method or
constructor to create a BigDecimal value.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Patricia Shanahan - 19 Apr 2006 23:44 GMT
> I have a bigdecimal  and a double value,say
>
[quoted text clipped - 6 lines]
>
> 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
EJP - 20 Apr 2006 03:04 GMT
> As already said, non-integer powers in BigDecimal would be a significant
> project.

Indeed. I implemented it years ago for a COBOL compiler and it took
several weeks. Even finding the constants for the polynomials is a
project in itself: for example, they are in a book which is out of print
and selling for large enough prices that I don't want to name it ;-) or
you can compute them yourself but then you have just started several
*more* projects.
manzur - 20 Apr 2006 05:58 GMT
> > I have a bigdecimal  and a double value,say
> >
[quoted text clipped - 15 lines]
>
> 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)
Patricia Shanahan - 20 Apr 2006 06:30 GMT
>>>I have a bigdecimal  and a double value,say
>>>
[quoted text clipped - 21 lines]
> 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
Chris Uppal - 20 Apr 2006 11:11 GMT
> 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
Roedy Green - 20 Apr 2006 06:34 GMT
> 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.  
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.