Matthew MacMillan
Matthew MacMillan

Reputation: 23

Math.pow() coming out wrong

I'm trying to create a JavaScript version of this equation that I got working in Excel:

Excel version:

10000*(1+0.06)^30+6000*(((1+0.06)^30-1)/0.06)

This calculates out to 531784.029

JavaScript version:

console.log(10000*(1+0.06)^30+6000*(((1+0.06)^30-1)/0.06));

Returns: 2789622

Or if I try to use Math.pow():

console.log(10000*(Math.pow(1+.06),30)+6000*(Math.pow((1+.06),30-1)/.06));

Returns: 841838.7898974773

I'm completely stumped after about 6 hours. How do I recreate that excel calculation and get the same number?

Upvotes: 1

Views: 583

Answers (1)

Frank Orellana
Frank Orellana

Reputation: 1898

For the same behavior as in excel you just have the wrong order of operators and wrong parameters to the Math.powfunction in js, it should be like this:

console.log(10000*(Math.pow(1+.06,30))+6000*((Math.pow(1+.06,30)-1)/0.06));

the ^ operator in javascript is not the same as in excel, in js it means a bitwise xor operation, while in excel it means the same as the Math.pow.

The Math.powfunction in javascript takes two parameters, so for example to make the same operation as in excel 1.06^30 you would do Math.pow(1.06,30)in javascript.

by the way maybe is just something you put for the example but you dont need to add 1+.06you could just write 1.06:

 console.log(10000*(Math.pow(1.06,30))+6000*((Math.pow(1.06,30)-1)/0.06));

Upvotes: 3

Related Questions