= 0) { throw new \Exception('Function values at endpoints must have opposite signs.'); } $c = $b; $iteration = 0; $sign = 1; while (($y = \abs($func($c))) > self::EPSILON && $iteration < $maxIterations) { $fa = $func($a); $fb = $func($b); if ($y === 0.0) { return $c; } if ($fa < 0) { $c = $sign === (int) ($y >= 0) ? (0.5 * $a * $fb - $b * $fa) / (0.5 * $fb - $fa) : ($a * $fb - $b * $fa) / ($fb - $fa); $b = $c; } else { $c = $sign === (int) ($y >= 0) ? ($a * $fb - 0.5 * $b * $fa) / ($fb - 0.5 * $fa) : ($a * $fb - $b * $fa) / ($fb - $fa); $a = $c; } $sign = (int) ($y > 0); ++$iteration; } return $c; } }