Skip to content

Instantly share code, notes, and snippets.

@pilotgeraldb
Last active May 31, 2016 19:57
Show Gist options
  • Save pilotgeraldb/bc512dc2c6d74bc1a4166d53d8cfe61e to your computer and use it in GitHub Desktop.
Save pilotgeraldb/bc512dc2c6d74bc1a4166d53d8cfe61e to your computer and use it in GitHub Desktop.
a javascript implementation using a taylor series to approximate the square root of a number (my own original work)
function squareRoot(value, precision)
{
function calcSeedVal(value, precision)
{
var a = 0;
var result = 0;
if(value > 9)
{
a = value / Math.pow(10,precision);
if(a >= 10)
{
result = 600;//6 * (Math.pow(10,2));
}
else
{
result = 100;//2 * (Math.pow(10,2));
}
}
else
{
result = value / 10;
}
return result;
};
var result = calcSeedVal(value, precision);
for(var i = 0; i < precision; i++)
{
result = (0.5) * (result + (value / result));
};
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment