-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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