note 86754 deleted from function.sqrt by danbrown
| From: | danbrown@php.net | Date: | Sun, 02 Nov 2008 14:47:52 +0000 |
| Subject: | note 86754 deleted from function.sqrt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146046@lists.php.net to get a copy of this message | ||
Note Submitter: luruke
----
My interpretation of sqrt() function:
<?php
function __strlen($text){
$i=0; while(isset($text[$i])) $i++;
return $i;
}
function __round($float, $offset){
$float = (string) $float;
for($temp='', $dot=false, $i=0; $i<__strlen($float); $i++){
if($dot) $offset--;
if($float[$i] == ".") $dot = true;
$temp .= $float[$i];
if($offset == 0) break;
}
return (float) $temp;
}
function __sqrt($num){
$precision = 9;
for($tmp=1;;){
$sqrt = ($tmp+$num/$tmp)/2;
if(__round($sqrt*$sqrt,$precision)==$num)
return __round($sqrt,$precision);
else
$tmp = $sqrt;
}
}
print __sqrt(569432981)."\n";
?>