Excel formula calculations into JavaScript [duplicate] - javascript

This question already has answers here:
JavaScript exponents
(8 answers)
Closed 4 years ago.
I have a formula into my excel sheet, now I am making that formula using JavaScript. I made a code exact like written on excel sheet but in JavaScript I am getting different result, I mean wrong result.
Excel formula
= H4*((((1+H7)^H8-1)/H7)*(1+H7))
JavaScript
var contribute = 1000;
var cum_rate = 0.001666667;
var num_periods = 480;
var fvc = contribute * ((((1 + cum_rate) ^ num_periods - 1) / cum_rate) * (1 + cum_rate));
console.log(fvc);
Result of this calculations should be 735659.68 but here I am getting wrong result, can you guys help me out what I am doing wrong here?

The karat doesn't mean exponent. You need to use Math.pow(base, exp) to evaluate the expression correctly:
var fvc = contribute*( ((Math.pow( (1+cum_rate), num_periods ) - 1)/cum_rate)*(1+cum_rate) )

Related

Subtraction problems in JavaScript [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 6 years ago.
If one subtracts 5 from 8.8, the actual result is: 3.8 but in my code it gives 3.8000000007. Why it this? Could anyone kindly provide a valuable insight? Thanks in advance.
It's working :
var p = 8.8 - 5;
console.log(p.toFixed(1));
I think there is nothing wrong in using "toFixed()". Why you are looking for an answer without using it?
Nervertheless you could do it the mathematical way:
var result = (8.8 * 10 - 5 * 10) / 10;
console.log(result);
The Javascript Engine is only trying to help you out. If it finds a Floating Point Number amongst the Numbers supplied in your Arithmetic Operations, it will automatically parse the Result to a Floating Point Number as well - (with the highest possible Precision).
Therefore, to get around this Eager but Unsolicited Help of the Javascript Engine; you'd need to manually call: toFixed() on the Resulting Value like so:
var p = 8.8 - 5;
console.log( p.toFixed(1) ); //<== 3.8
Alternatively, You may extend the Number Prototype and add your Unique Functionality if that is what you desire. However, this is not at all advised!!!
<script type="text/javascript">
Number.prototype.precise = function(){
return this.toFixed(1);
};
Number.prototype.floatSubtract = function(num){
return (this - num).toFixed(1);
};
var p1 = (8.8 - 5).precise();
var p2 = 8.8.floatSubtract(5);
console.log(p1); //< 3.8
console.log(p2); //< 3.8
</script>
Try this. I think this will be the solution.
<!doctype HTML>
<html>
<head>
<script>
num = 8.8 - 5;
console.log(Math.round(num * 100) / 100)
</script>
</head>
</html>
In the below picture, Calculation part is there. Kindly Check it.

Python/Javascript number precision [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
Probably this question is already existed somewhere but can't find my specific issue.
I have a two numbers that I want to add and I don't want to use round() function since I am working with money.
1.70 + 1.44 = 3.14 //Returns 3.1399999999999997
1.70 + 1.45 = 3.15 //Returns the correct answer
1.70 + 1.37 = 3.07 //Returns 3.0700000000000003
I tried it in Javascript but still the same issue.
Since you're working with money, always use the decimal.Decimal class in Python.
Use parseFloat to precision :
parseFloat(number).toFixed(precision);
E.g.
parseFloat(1.70 + 1.37).toFixed(2); // RETURN 3.07
one way you can achieve this is to multiply the values by (10 * precision) that you need and then divide the result by (10 * precision).
((1.70 * 100) + (1.37 * 100))/100 = 3.07
((1.70 * 100) + (1.45 * 100))/100 = 3.15
((1.70 * 100) + (1.44 * 100))/100 = 3.14
you can add a method to do this for you to keep it simple.

How can I use an exponent in javascript

I am trying to build a simple loan calculator using jQuery for practice. I have most of the code down, but am having trouble inserting the exponent for the formula. I am aware of the Math.pow, but not sure if I am implementing it properly. Here is the loan formula:
P ( r / 12 ) / (1 - ( 1 + r / 12 ) ^-m )
P = principal
r = interestRate
-m = loan term in months
Here is my code:
var months = ("#loanTerm" * -1);
var calc = Math.pow(1 + (interestRate / 12), months);
Here is the HTML:
Loan Term(Months): <input class="userInput" id="loanTerm" type='number'>
Not sure if I am doing something wrong with the Math.pow, or if there is a way I am able to simply set the months to an integer, I'm relatively new to jQuery so any help would be much appreciated. Thanks in advance.
After the exchange of comments, I'm fairly sure the underlying question is how to access the value of a form input with jQuery.
In jQuery you can get the value of an <input> with
$("#loanTerm").val()
In vanilla JavaScript you can use
document.getElementById("loanTerm").value
You can then explicitly convert it to an integer and make it negative like so (in jQuery):
var months = parseInt($("#loanTerm").val(), 10) * (-1);
To test if months is NaN (the user didn't enter anything, or entered non-numeric data), you can use isNaN().
Finally, here is a testbed you can experiment with to confirm the equation is working as expected. I hope this helps.

Convert equation string to equation [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Running an equation with Javascript from a text field
How can I convert the following:
var n = "2x^3+3x+6";
To
var x = a number
var n = 2x^3+3x+6;
In JavaScript?
Quite hard to guess what the exact requirements and the context are, but if you want to roughly stick to the grammar demonstrated by your variable I'd suggest using a math expression parser.
Using js-Expression-eval, it could look like this:
var formula = "2*x^3+3*x+6";
var expression = Parser.parse(formula);
var result = expression.evaluate({ x: 3 });
Run the Fiddle
Should you want to have your own grammar - to leave out the * symbols for multiplication with variables, for example - you'll have to roll your own parser, for example using something like jison.
var x = a number;
var n = eval("2*Math.pow(x,3)+3*x+6")

How to get x of 2^x=8000? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the opposite of JavaScript's Math.pow?
2^x=i
Given i, how can we calculate x using Javascript?
You want to take the logarithm of 8000. JS has the Math.log function which uses base e, you want base 2 so you can write Math.log(8000) / Math.log(2) to get the logarithm of 8000 base 2, which is equal to x.
You need the logarithm from the Math object. It does not provide a base 2 log so do the conversion:
var x = Math.log(8000) / Math.log(2);
Reference to the javascript Math object.
In the more general case we calculate 2^x = i this way:
var i; // Some number
var x = Math.log(i) / Math.log(2);

Categories