What is the command to do a power of a value

Discussion in 'Java' started by xvictoryeohx, Jul 30, 2012.

  1. xvictoryeohx

    xvictoryeohx Guest

    From:

    C=L(1+i/100)power of n

    i am stuck here
    for example square root is Math.sqrt(x) How do i do Power of a value?

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    xvictoryeohx, Jul 30, 2012
    #1
    1. Advertising

  2. To: xvictoryeohx
    From: glen herrmannsfeldt <>

    wrote:
    > C=L(1+i/100)power of n


    > i am stuck here
    > for example square root is Math.sqrt(x)
    > How do i do Power of a value?


    Math.pow().

    -- glen

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    glen herrmannsfeldt, Jul 30, 2012
    #2
    1. Advertising

  3. xvictoryeohx

    xvictoryeohx Guest

    To: glen herrmannsfeldt
    From:

    On Monday, July 30, 2012 12:31:35 PM UTC+8, glen herrmannsfeldt wrote:
    > wrote:
    >
    > > C=L(1+i/100)power of n

    >
    >
    >
    > > i am stuck here

    >
    > > for example square root is Math.sqrt(x)

    >
    > > How do i do Power of a value?

    >
    >
    >
    > Math.pow().
    >
    >
    >
    > -- glen


    Thanks!

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    xvictoryeohx, Jul 30, 2012
    #3
  4. xvictoryeohx

    Roedy Green Guest

    To: xvictoryeohx
    From: Roedy Green <>

    On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), wrote, quoted
    or indirectly quoted someone who said :

    >C=L(1+i/100)power of n
    >
    >i am stuck here
    >for example square root is Math.sqrt(x)
    >How do i do Power of a value?


    see http://mindprod.com/jgloss/power.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The greatest shortcoming of the human race is our inability to understand the
    exponential function.
    ~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)
    http://www.youtube.com/watch?v=F-QA2rkpBSY

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    Roedy Green, Jul 30, 2012
    #4
  5. To: Roedy Green
    From: Andreas Leitgeb <>

    On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), wrote:
    > C=L(1+i/100)power of n


    x ^ n = exp ( log(x) * n ) | x = (1 + i/100)
    = exp ( log( 1 + i/100 ) * n)
    = exp ( log1p ( i/100 ) * n)

    If you're doing more calculations with same interest-rate but different
    periods, then you may want to calculate
    double logBase = Math.log1p( i / 100 );
    once, and use that for the individual calculations:
    C = L * Math.exp( logBase * n )

    The gist of this response is, that for the kind of base (1+i/100), you better
    separate the pow operation out into log and exp, and actually use log1p on
    (i/100) instead of log on (1+i/100) for efficiency's and precision's sake.

    For "Math.log1p" see:
    http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#log1p(double)

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    Andreas Leitgeb, Jul 30, 2012
    #5
  6. To: Andreas Leitgeb
    From: Patricia Shanahan <>

    On 7/30/2012 6:22 AM, Andreas Leitgeb wrote:
    > On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), wrote:
    >> C=L(1+i/100)power of n

    >
    > x ^ n = exp ( log(x) * n ) | x = (1 + i/100)
    > = exp ( log( 1 + i/100 ) * n)
    > = exp ( log1p ( i/100 ) * n)
    >
    > If you're doing more calculations with same interest-rate but
    > different periods, then you may want to calculate
    > double logBase = Math.log1p( i / 100 );
    > once, and use that for the individual calculations:
    > C = L * Math.exp( logBase * n )
    >
    > The gist of this response is, that for the kind of base (1+i/100),
    > you better separate the pow operation out into log and exp, and
    > actually use log1p on (i/100) instead of log on (1+i/100) for
    > efficiency's and precision's sake.
    >
    > For "Math.log1p" see:
    > http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#log1p(double%2

    9
    >


    I am curious about why you expect this to be more precise than: "The computed
    result must be within 1 ulp of the exact result. Results must be
    semi-monotonic." (From the pow description). Or do you know of cases where
    Math.pow gets an over-large rounding error?

    Note that I am not disagreeing with your method for calculating a power of a
    number slightly greater than 1, just questioning whether doing it explicitly
    gets more precise answers than using Math.pow.

    Patricia

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    Patricia Shanahan, Jul 30, 2012
    #6
  7. Patricia Shanahan <patricia.shanahan@1:261/38.remove-rf4-this> wrote:

    (snip, someone wrote)
    >> x ^ n = exp ( log(x) * n ) | x = (1 + i/100)
    >> = exp ( log( 1 + i/100 ) * n)
    >> = exp ( log1p ( i/100 ) * n)


    (snip)

    > I am curious about why you expect this to be more precise than:
    > "The computed result must be within 1 ulp of the exact result.
    > Results must be semi-monotonic." (From the pow description).
    > Or do you know of cases where Math.pow gets an over-large
    > rounding error?


    I didn't know about the log1p function. (Does Java have one?)

    As x gets small log(1+x) loses precision. Consider:

    log(1+1e-60)

    The x87 instruction set has an instruction for evaluating log(1+x)
    and one for evaluating exp(x-1). Most HLLs don't.

    -- glen
    glen herrmannsfeldt, Jul 30, 2012
    #7
  8. glen herrmannsfeldt <> wrote:
    > and one for evaluating exp(x-1).


    That is exp(x)-1, not exp(x-1), just for the record ;-)
    Andreas Leitgeb, Jul 31, 2012
    #8
  9. To: Patricia Shanahan
    From: Andreas Leitgeb <>

    Patricia Shanahan <> wrote:
    > On 7/30/2012 6:22 AM, Andreas Leitgeb wrote:
    >> The gist of this response is, that for the kind of base (1+i/100),
    >> you better separate the pow operation out into log and exp, and
    >> actually use log1p on (i/100) instead of log on (1+i/100) for
    >> efficiency's and precision's sake.
    >> For "Math.log1p" see:
    >> http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#log1p(double%

    29
    >
    > I am curious about why you expect this to be more precise than: "The
    > computed result must be within 1 ulp of the exact result. ..."


    Well, one ulp of i/100 is likely smaller than one ulp of 1+i/100 (at least it
    is for 0 <= i <= 100, the typical range for interest rates). It's like
    calculating sin(0.0) versus sin(Math.PI), where sin() makes the same promise
    wrt precision up to an ulp.

    If the OP had been interested in the interest value alone, i.e. in
    I = L*( (1+i/100)^n ) - L
    then using log1p() and expm1() probably would beat the precision of pow() by,
    um, a few decimal digits, depending of course on the values of i and n.

    Anyway, I think it's good to know that log1p() and expm1() exist, even if the
    example at hand doesn't now seem to cry out for them as loudly as I thought it
    did on first read.

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    Andreas Leitgeb, Jul 31, 2012
    #9
    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. Replies:
    8
    Views:
    357
    Mark Dickinson
    Apr 17, 2008
  2. Replies:
    5
    Views:
    153
    Andreas Leitgeb
    Jul 31, 2012
  3. bugbear
    Replies:
    0
    Views:
    136
    bugbear
    Jul 30, 2012
  4. xvictoryeohx
    Replies:
    11
    Views:
    201
    Andreas Leitgeb
    Jul 31, 2012
  5. xvictoryeohx
    Replies:
    10
    Views:
    218
    Andreas Leitgeb
    Jul 31, 2012
Loading...

Share This Page