OSSU-computer-science/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/04-machine-interpretation-of-program/lec04.js
2014-06-07 19:21:41 -03:00

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 );