Diriector_Doc

JavaScript Square Root Function

Jan 3rd, 2021 (edited)
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Why are there two `temp`s? Otherwise, it will repeat forever for some numbers.
  3.  * With a single `temp`, sqrt(3) with infinitely iterate between 1.7320508075688772 and 1.7320508075688774
  4.  */
  5. function sqrt(x){
  6.     let temp,
  7.     temp2,
  8.     i=2*x/(x+1);
  9.     while(temp != i && temp2 != i){
  10.         temp2 = temp;
  11.         temp = i;
  12.         i = (i**2 + x)/(2*i)
  13.     }
  14.     return i
  15. }
  16.  
  17. // It may seem like I stole this from pastebin.com/umyk7xVB but trust me, this is mine.
Add Comment
Please, Sign In to add comment