I am trying to port a formula from numbers (Mac OS 10) to javascript, the original formula is thus:
(1−(1+(C37÷100)÷C39)^−C45)÷((C37÷100)÷C39)
I have written this:
var calcValueOne = (1 - (1 + (yield/100) / coupon_frequency) Math.sqrt() - coupons_until_maturity) / ( (yield/100) / coupon_frequency);
where yield = C37 and coupon_frequency = C39 and coupons_until_maturity = C45
I am getting the following error:
SyntaxError: Unexpected identifier 'Math'. Expected ')' to end a
compound expression.
My mathematics is not to hot and transposing this in to Javascript is proving to be a huge challenge for me, can anyone post a solution to this please?
EDIT
#Musa kindly added a response but the number it is returning is not what I expect so I thought I would expand on the values to see if that helps.
yield (C37) == 9.89
coupon_frequency (C39) == 4.00
coupons_until_maturity (C45) == 15.00
The number I am expecting is 12.41 but I am getting -607.1703544847592 the javascript now looks like this:
var calcValueOne = (1 - Math.sqrt(1 + (yield/100) / coupon_frequency) - coupons_until_maturity) / ( (yield/100) / coupon_frequency);
Again for clarity this is the original (excel / numbers) formula:
The ^ sign is the exponent function not the square root so use Math.pow to calculate it
var yield = 9.89;
var coupon_frequency= 4.00;
var coupons_until_maturity = 15.00
var calcValueOne = (1 - Math.pow((1 + (yield/100) / coupon_frequency), - coupons_until_maturity)) / ( (yield/100) / coupon_frequency);
document.body.innerHTML = calcValueOne;
Related
I have a function that I need to handle in Javascript:
equipment * ((rate/12) / ((1-(1+(rate/12))^-term)))
How do I convert it (^) into Javascript?
This is what it looks like so far:
const calculation = equipment * ((rate / 12) / ((1 - (1 + (rate / 12)) - term)));
JavaScript has an operator for it:
console.log(3 ** 5);
Try using this:
let result = Math.pow(3, 5)
//or
let result = 3 ** 5
This is equivalent to 3^5
How to calculate fibonacci function by math formula
I have try this formula but not work:
fib(n) = ((1 + 5^0.5) / 2)^n - ((1 - 5^0.5) / 2)^n / 5^0.5
const fib=(n)=>{
return ((1+(5**0.5))/2)**n-((1-(5**0.5))/2)**n/(5**0.5)
}
Any one know how to do?Thanks.
The formula is correct you just need to put some extra ().
const fib=(n)=>{
return (((1+(5**0.5))/2)**n-(((1-(5**0.5))/2)**n))/(5**0.5)
}
for(let i = 0;i<9;i++){
console.log(fib(i))
}
First thing I'd do is define φ
var φ = (1 + 5 ** 0.5) / 2;
Then a slightly shorter form is:
var fib = (n) => (φ ** n - ((-φ) ** -n)) / (2 * φ - 1);
Because you want an integer as a result, you could also throw in the call to Math.round().
You seem to be trying to recreate Binet's Formula. You can break down the formula a little like so, such that is becomes more readable:
const fib = n => {
const alpha = 5**0.5
const beta = alpha / 2;
return (1 / alpha) * ((0.5 + beta) ** n - (0.5 - beta) ** n);
}
console.log(fib(10));
I'm doing a Unity project where I have the need to convert UTM coordinates to latitudes and longitudes. I have tried several C# solutions, but none of them were accurate enough. But I found some Javascript code that gives out the exact result I'm looking for (https://www.movable-type.co.uk/scripts/latlong-utm-mgrs.html). The problem is, when I turned the code into C#, it gives out a different result. Here are the pieces of code I see the problem in:
Javascript:
var a = 6378137;
var f = 1/298.257223563;
var e = Math.sqrt(f*(2-f));
var n = f / (2 - f);
var n2 = n*n, n3 = n*n2, n4 = n*n3, n5 = n*n4, n6 = n*n5;
var A = a/(1+n) * (1 + 1/4*n2 + 1/64*n4 + 1/256*n6);
And the C# code I converted myself:
var a = 6378137;
var f = 1 / 298.257223563;
var e = Math.Sqrt(f * (2 - f));
var n = f / (2 - f);
var n2 = n * n;
var n3 = n2 * n;
var n4 = n3 * n;
var n5 = n4 * n;
var n6 = n5 * n;
var A = a / (1 + n) * (1 + 1 / 4 * n2 + 1 / 64 * n4 + 1 / 256 * n6);
They should be identical, but the value of A is different. In Javascript it's 6367449.145823415 (what I want), but C# gives 6367444.6571225897487819833001. I could just use the Javascript code in my project, but conveniently Unity stopped supporting Javascript just last year.
The inputs are the same for both functions. What could be the issue here?
You have an issue in the last expression
var A = a / (1 + n) * (1 + 1 / 4 * n2 + 1 / 64 * n4 + 1 / 256 * n6);
In C# 1 / 4 * n2 is evaluated to 0 since 1 and 4 are considered as integers by default and integer division 1 / 4 gives 0. Same thing happens to 1 / 64 * n4 and 1 / 256 * n6. But in JavaScript there are only 64-bit floating point numbers, so 1 / 4 is evaluated to 0.25.
Possible workaround:
var A = a / (1 + n) * (1 + 1 / 4.0 * n2 + 1 / 64.0 * n4 + 1 / 256.0 * n6);
Now answers seem to be exactly the same.
Note: as #Lithium mentioned, you may find more elegant to add d rather than .0 to the end of the number to indicate it as double.
This likely has to do with the lack of handling for floating point numbers in javascript. C# handles it fine but not Javascript. If you would like to know more about some of the quirks in Javascript numbers check out the link below.
https://www.w3schools.com/js/js_numbers.asp
In your C# use all your variables as type double. These are twice as accurate as the standard C# floats which I assume it is using by default.
This should give you an identical answer.
Most languages have the concept of a single precision floating point number and a double precision floating point number (32 bit vs 64 bit). Due to JavaScript being a Non-strongly typed language I'm assuming it's picking up the fact that it should use 64 bits for your numbers whereas C# needs to be told that. You should also avoid var in C# wherever possible and use the actual type, to avoid situations like this :p
Im trying to figure out how to write a JavaScript program that computes and outputs the volume of a regular tetrahedron. This is how far I got but it seems to get a error and not compute the right numbers after.
The equation for the triangle is
v = a3
6 √ 2
Sorry about the code i dont know how to post things on here very effectively. So this is my variables
var a = parseFloat(document.getElementById('length').value);
var b = (a * a * a) / 6 * Math.sqrt(2)
You are very close. You are missing some parenthesis around 6 * Math.sqrt(2)
Your code is doing (a*a*a) / 6 and then multiplying that result by the square root of 2.
You can read up on Operator Precedence
var a = 4;
var b = (a * a * a) / (6 * Math.sqrt(2))
console.log(b);
You can also use Math.pow()
var a = 4;
var b = Math.pow(a,3) / (6 * Math.sqrt(2))
console.log(b);
A friend showed me that (at least, in the google chrome console) the following statement prints true:
1/Math.pow(0.9999999999999999, Number.MAX_SAFE_INTEGER) === Math.E
And indeed, 1/Math.pow(0.9999999999999999, Number.MAX_SAFE_INTEGER) is 2.718281828459045.
This can't be a coincidence?!
Could someone explain what is going on behind the scenes to make this work?
According to wolfram alpha, the correct value should be approximately 1/0.40628 which is approximately 2.4613566998129373 -- very far off from Math.E. (I am assuming that wolframalpha is more precise in its calculations than javascript, but I may be wrong).
Any explanation would be appreciated.
Bonus: What is the true approximate mathematical value of that expression, I wonder?
I found this:
n = 0.0000000000000001
(1 - n)^MAX_INT = 1 + (MAX_INT choose 2) * n + (MAX_INT choose 3) * n^2 + ... + n^MAX_INT
but I have no idea how to approximate that.
I tested the above expression in wolfram alpha and got 2.46 as well.
pow(x, y) is typically computed as exp(log(x) * y), so let's start there.
We have:
x = 0.9999999999999999, which rounds to x = 1 - eps (where eps == 2^-53).
y = 2^53 - 1 i.e. y = 1 / eps (approximately).
So we're actually calculating exp(log(1 - eps) * 1/eps).
The Taylor series expansion of log(1 - k) is -k - k^2/2 - ..., but in our case all the higher-order terms will be truncated.
So we have exp(-eps / eps), or exp(-1), which is 1 / e.
Demonstration:
1 - 0.9999999999999999 // 1.1102230246251565e-16
Math.log(1 - 1.1102230246251565e-16) // -1.1102230246251565e-16
1 / Number.MAX_SAFE_INTEGER // 1.1102230246251568e-16
This arises from the original characterisation of e as:
Then using the property that:
MAX_SAFE_INTEGER = 253-1, and
0.9999999999999999 rounds to 1 - 2-53
then
1/(1-2-53) = 1 + 2-53/(1-2-53) = 1 + 1/(253-1)
Therefore,
1/(1-2-53)253-1 = [1 + 1/(253-1)]253-1
which is very close to e.