mirror of
https://github.com/ossu/computer-science.git
synced 2025-07-01 17:56:46 -04:00
16 lines
359 B
JavaScript
16 lines
359 B
JavaScript
var x = 0.5;
|
|
var epsilon = 0.01;
|
|
var low = 0;
|
|
var high = x;
|
|
var ans = ( high + low ) / 2;
|
|
|
|
while ( Math.abs( Math.pow( ans, 2 ) ) >= epsilon && ans <= x ) {
|
|
console.log( 'ans = ' + ans + ' low = ' + low + ' high = ' + high );
|
|
|
|
if ( Math.pow( ans, 2 ) < x )
|
|
low = ans;
|
|
else
|
|
high = ans;
|
|
}
|
|
|
|
console.log( ans + ' is close to square root of ' + x );
|