Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.java.* > Newsgroup comp.lang.javascript

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 12-21-2006, 10:24 PM
grahamhow424@hotmail.com
Guest
 
Posts: n/a
Default Exponentiation in a Formula

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!

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 12-21-2006, 11:10 PM
David Golightly
Guest
 
Posts: n/a
Default Re: Exponentiation in a Formula


grahamhow424@hotmail.com wrote:
> 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!


The caret operator (^) is the bitwise XOR operator in JavaScript. Use
Math.pow for exponents:

Math.pow(2,3)
=> 8

Reply With Quote
  #3 (permalink)  
Old 12-21-2006, 11:21 PM
-Lost
Guest
 
Posts: n/a
Default Re: Exponentiation in a Formula

<grahamhow424@hotmail.com> wrote in message
news:1166743460.217790.57400@79g2000cws.googlegrou ps.com...
> 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


Reply With Quote
  #4 (permalink)  
Old 12-21-2006, 11:23 PM
grahamhow424@hotmail.com
Guest
 
Posts: n/a
Default Re: Exponentiation in a Formula

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


Reply With Quote
  #5 (permalink)  
Old 12-22-2006, 01:16 AM
Malkavian
Guest
 
Posts: n/a
Default Re: Exponentiation in a Formula


<grahamhow424@hotmail.com> wrote in message
news:1166743460.217790.57400@79g2000cws.googlegrou ps.com...
> Hi
>
> I am trying to figure out how to duplicate a, financial, calculation
> that uses the caret, Exponentiation.


*snip*

There maybe something here that'll help.

http://www.comptechdoc.org/independe.../javamath.html

HTH


Reply With Quote
  #6 (permalink)  
Old 12-22-2006, 02:05 AM
-Lost
Guest
 
Posts: n/a
Default Re: Exponentiation in a Formula

<grahamhow424@hotmail.com> wrote in message
news:1166747009.813092.43070@42g2000cwt.googlegrou ps.com...
> 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


Reply With Quote
  #7 (permalink)  
Old 12-22-2006, 03:27 AM
grahamhow424@hotmail.com
Guest
 
Posts: n/a
Default Re: Exponentiation in a Formula

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:
> <grahamhow424@hotmail.com> wrote in message
> news:1166747009.813092.43070@42g2000cwt.googlegrou ps.com...
> > 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


Reply With Quote
  #8 (permalink)  
Old 12-22-2006, 04:09 PM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: Exponentiation in a Formula

In comp.lang.javascript message
<1166743460.217790.57400@79g2000cws.googlegroups.c om>, Thu, 21 Dec 2006
15:24:20, grahamhow424@hotmail.com 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.
Reply With Quote
  #9 (permalink)  
Old 12-22-2006, 10:25 PM
-Lost
Guest
 
Posts: n/a
Default Re: Exponentiation in a Formula

<grahamhow424@hotmail.com> wrote in message
news:1166761670.483712.232580@f1g2000cwa.googlegro ups.com...

> 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


Reply With Quote
 
Reply

Thread Tools
Display Modes

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
Re: Formula for calculating contractor rates Gary McQuown Newsgroup comp.soft-sys.sas 0 06-30-2006 02:26 PM
SAS EXCEL Formula O/P Question Sarav Newsgroup comp.soft-sys.sas 0 01-17-2006 07:08 PM
Re: R-square formula Arthur Tabachneck Newsgroup comp.soft-sys.sas 0 10-11-2005 12:45 PM
Creating a Formula in Excel (non-SAS Question) Srna, Carol Newsgroup comp.soft-sys.sas 1 09-28-2005 01:35 PM
Re: Question on Proc SQL - correct age formula Mike Rhoads Newsgroup comp.soft-sys.sas 0 05-25-2005 08:23 PM



All times are GMT. The time now is 05:58 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.