Exponentiation in a Formula

Discussion in 'Javascript' started by grahamhow424@hotmail.com, Dec 21, 2006.

  1. Guest

    Hi

    I am trying to figure out how to duplicate a, financial, calculation
    that uses the caret, Exponentiation.

    Here's the formula...

    A = 0.0755
    B = 34
    C = 50000
    D = 22448

    result = ((C-D)*A/12)/(1-(1+A/12)^(-B))+D*A/12

    It calculates loan repayments based on interest rate (A), number of
    payments (B), total loan amount (C) and a residual amount (D).

    This calculation will work when the caret (Exponentiation) is used,
    however this is not available in Javascript. So, to get this formula to
    work in Javascript some other method needs to be used.

    I have found examples of how to do Exponentiation, like this:

    /***********************************************************/

    function powmod(base,exp,modulus)
    {
    var accum=1, i=0, basepow2=base;
    while ((exp>>i)>0)
    {
    if(((exp>>i) & 1) == 1){accum = (accum*basepow2) % modulus;};
    basepow2 = (basepow2*basepow2) % modulus;
    i++;
    };
    return accum;
    }

    /***********************************************************/

    This function comes from here
    http://www.math.umbc.edu/~campbell/N...pt.html#PowMod (some handy stuff
    there) but I can't replicate the calculation I have posted using a
    function like that.

    Anyone know how to do this?

    Thanks!
     
    , Dec 21, 2006
    #1
    1. Advertising

  2. -Lost Guest

    <> wrote in message
    news:...
    > Hi
    >
    > I am trying to figure out how to duplicate a, financial, calculation
    > that uses the caret, Exponentiation.
    >
    > Here's the formula...
    >
    > A = 0.0755
    > B = 34
    > C = 50000
    > D = 22448
    >
    > result = ((C-D)*A/12)/(1-(1+A/12)^(-B))+D*A/12


    result = ((C-D)*A/12)/(1-Math.pow((1+A/12),(-B)))+D*A/12;

    -Lost
     
    -Lost, Dec 22, 2006
    #2
    1. Advertising

  3. Guest

    Hi David

    Thanks, I got it now.

    The formula I posted previously could translate in JS as...

    var A1 = 0.0755;
    var B1 = 34;
    var C1 = 50000;
    var D1 = 22448;

    alert(((C1 - D1) * A1 / 12) / (1 - Math.pow((1 + A1 / 12), -B1)) + (D1
    * A1 / 12));

    Thanks again!


    David Golightly wrote:
    > The caret operator (^) is the bitwise XOR operator in JavaScript. Use
    > Math.pow for exponents:
    >
    > Math.pow(2,3)
    > => 8
     
    , Dec 22, 2006
    #3
  4. -Lost Guest

    <> wrote in message
    news:...
    > Hi David
    >
    > Thanks, I got it now.
    >
    > The formula I posted previously could translate in JS as...
    >
    > var A1 = 0.0755;
    > var B1 = 34;
    > var C1 = 50000;
    > var D1 = 22448;
    >
    > alert(((C1 - D1) * A1 / 12) / (1 - Math.pow((1 + A1 / 12), -B1)) + (D1
    > * A1 / 12));


    result = ((C-D)*A/12)/(1-Math.pow((1+A/12),(-B)))+D*A/12;

    That is the exact same thing I wrote (a few minutes before you posted again).

    -Lost
     
    -Lost, Dec 22, 2006
    #4
  5. Guest

    Ahhhh -Lost

    You nailed it. Unfortunately I sorted it out while you were posting,
    wish you posted about 3 mins earlier!

    Anyway, thanks a lot, appreciate your input.

    -Lost wrote:
    > <> wrote in message
    > news:...
    > > Hi David
    > >
    > > Thanks, I got it now.
    > >
    > > The formula I posted previously could translate in JS as...
    > >
    > > var A1 = 0.0755;
    > > var B1 = 34;
    > > var C1 = 50000;
    > > var D1 = 22448;
    > >
    > > alert(((C1 - D1) * A1 / 12) / (1 - Math.pow((1 + A1 / 12), -B1)) + (D1
    > > * A1 / 12));

    >
    > result = ((C-D)*A/12)/(1-Math.pow((1+A/12),(-B)))+D*A/12;
    >
    > That is the exact same thing I wrote (a few minutes before you posted again).
    >
    > -Lost
     
    , Dec 22, 2006
    #5
  6. In comp.lang.javascript message
    <>, Thu, 21 Dec 2006
    15:24:20, wrote:
    >
    >I am trying to figure out how to duplicate a, financial, calculation
    >that uses the caret, Exponentiation.


    In Javascript, X = Base^Expo is actually bitwise exclusive-or, and
    X = Math.pow(Base, Expo) exponentiates. Read any relevant book.

    It's a good idea to read the newsgroup and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Dec 22, 2006
    #6
  7. -Lost Guest

    <> wrote in message
    news:...

    > Ahhhh -Lost
    >
    > You nailed it. Unfortunately I sorted it out while you were posting,
    > wish you posted about 3 mins earlier!
    >
    > Anyway, thanks a lot, appreciate your input.


    Heh. No problem!

    -Lost
     
    -Lost, Dec 22, 2006
    #7
    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. Steven T. Hatton

    An exponentiation function for int?

    Steven T. Hatton, Oct 13, 2004, in forum: C++
    Replies:
    14
    Views:
    737
    JXStern
    Oct 16, 2004
  2. Jeff Davis

    strange exponentiation performance

    Jeff Davis, Nov 23, 2003, in forum: Python
    Replies:
    0
    Views:
    335
    Jeff Davis
    Nov 23, 2003
  3. Tim Peters
    Replies:
    1
    Views:
    428
    Jeff Davis
    Nov 24, 2003
  4. elventear

    Decimal and Exponentiation

    elventear, May 19, 2006, in forum: Python
    Replies:
    7
    Views:
    632
    Tim Peters
    May 20, 2006
  5. exponentiation operator (lack of)

    , Dec 22, 2005, in forum: C Programming
    Replies:
    67
    Views:
    1,495
    Dave Thompson
    Jan 4, 2006
Loading...

Share This Page