Skip to content

Instantly share code, notes, and snippets.

@riicchhaarrd
Created December 31, 2021 06:59
Show Gist options
  • Select an option

  • Save riicchhaarrd/8b546d88c6e49c82f2318424e1a43d86 to your computer and use it in GitHub Desktop.

Select an option

Save riicchhaarrd/8b546d88c6e49c82f2318424e1a43d86 to your computer and use it in GitHub Desktop.
taylor expansion sin & factorial javascript
factorial = (x) =>
{
let total = 1;
for(let i = 1; i <= x; i+=2)
{
if(i + 1 > x)
total *= i;
else
total *= (i * (i + 1));
}
return total;
};
sin = (x) =>
{
let s = x;
let n = 3;
for(let i = 0; i < 32; ++i)
{
if(i % 2 == 0)
s -= Math.pow(x, n) / factorial(n);
else
s += Math.pow(x, n) / factorial(n);
n += 2;
}
return s;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment