calculating the a bigdecimal to the power of double

Discussion in 'Java' started by manzur, Apr 19, 2006.

  1. manzur

    manzur Guest

    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.
     
    manzur, Apr 19, 2006
    #1
    1. Advertising

  2. 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


    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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Apr 19, 2006
    #2
    1. Advertising

  3. manzur

    Danno Guest

    I don't think you can xor floating points just integers.
     
    Danno, Apr 19, 2006
    #3
  4. manzur

    Eric Sosman Guest

    manzur wrote On 04/19/06 12:15,:
    > 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


    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 ...)

    --
     
    Eric Sosman, Apr 19, 2006
    #4
  5. manzur

    Roedy Green Guest

    On 19 Apr 2006 09:15:41 -0700, "manzur" <> wrote,
    quoted or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Apr 19, 2006
    #5
  6. manzur

    Roedy Green Guest

    On 19 Apr 2006 09:59:22 -0700, "Danno" <>
    wrote, quoted or indirectly quoted someone who said :

    >I don't think you can xor floating points just integers.


    He means ^ to represent power.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Apr 19, 2006
    #6
  7. 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
     
    Patricia Shanahan, Apr 19, 2006
    #7
  8. manzur

    Danno Guest

    There must've been some invisible thing on there. ;) hehe
     
    Danno, Apr 20, 2006
    #8
  9. manzur

    EJP Guest

    Patricia Shanahan wrote:
    >
    > 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.
     
    EJP, Apr 20, 2006
    #9
  10. manzur

    Danno Guest

    Ah, the title!

    Shit, I never look there. ;)
     
    Danno, Apr 20, 2006
    #10
  11. manzur

    manzur Guest

    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)
     
    manzur, Apr 20, 2006
    #11
  12. 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
     
    Patricia Shanahan, Apr 20, 2006
    #12
  13. manzur

    Roedy Green Guest

    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.
     
    Roedy Green, Apr 20, 2006
    #13
  14. manzur

    Chris Uppal Guest

    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
     
    Chris Uppal, Apr 20, 2006
    #14
  15. manzur

    andyt

    Joined:
    Dec 16, 2010
    Messages:
    1
    Implementation of BigDecimal to the power of BigDecimal

    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
     
    andyt, Dec 16, 2010
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,506
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    3
    Views:
    1,233
  3. Replies:
    8
    Views:
    371
    Mark Dickinson
    Apr 17, 2008
  4. Stanimir Stamenkov
    Replies:
    4
    Views:
    2,591
    Stanimir Stamenkov
    Jul 18, 2008
  5. Erik the Red
    Replies:
    4
    Views:
    174
    Chris Pine
    Jul 29, 2005
Loading...

Share This Page