forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareRoot.js
More file actions
Latest commit
23 lines (21 loc) · 545 Bytes
/
Copy pathSquareRoot.js
File metadata and controls
23 lines (21 loc) · 545 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
* Author: Rak Laptudirm
*
* https://en.wikipedia.org/wiki/Newton%27s_method
*
* Finding the square root of a number using Newton's method.
*/
functionsqrt(num,precision=4){
if(!Number.isFinite(num)){
thrownewTypeError(`Expected a number, received ${typeofnum}`)
}
if(!Number.isFinite(precision)){
thrownewTypeError(`Expected a number, received ${typeofprecision}`)
}
letsqrt=1
for(leti=0;i<precision;i++){
sqrt-=(sqrt*sqrt-num)/(2*sqrt)
}
returnsqrt
}
export{sqrt}