<?php /** * $t 是误差范围值 */ function squre($number ,$t){ $x1 = $number; $x2 = $number/2; while( abs($x1 - $x2) > $t){ $x1 = $x2; $x2 = ($x1 + $number/$x1)/2; } return $x2; } echo squre(5,0.1);
基本思路是使用二分法
<?php /** * $t 是误差范围值 */ function squre($number ,$t){ $x1 = $number; $x2 = $number/2; while( abs($x1 - $x2) > $t){ $x1 = $x2; $x2 = ($x1 + $number/$x1)/2; } return $x2; } echo squre(5,0.1);
基本思路是使用二分法