(untitled) · Oct 25, 2015
How to calculate the square root using Babylonian method.
0Sign in to vote or save
This site does not allow itself to be embedded. You can still read it on the original site — the toolbar below keeps your place in the directory.
Babylonian method for calculating square root of a number. function sqrt(n) { if ( ! ( typeof n === 'number' && n >= 0 && ! isNaN (n))) { return NaN ; } else if (n === 0 ) { return 0 ; } else if (n === Infinity ) { return Infinity ; } var val = n; while ( true ) { var last = val; val = (val + n / val) * 0.5 ; if ( Math .abs(val - last) < 1e-9 ) { break ; } } return val; } Usage: console.log(sqrt(…
Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.