[ create a new paste ] login | about

Link: http://codepad.org/eI4ykyQU    [ raw code | output | fork ]

PHP, pasted on May 9:
<?php

function _pow($n, $exp)
{
	$result = pow($n, intval($exp)); // bcmath casts $exp to (int)

	while (fmod($exp, 1) > 0) // does $exp have a fracional part higher than 0?
	{
                $e = 1 / fmod($exp, 1); // lets convert the fracional part into a root (2.5 -> 1 / 0.5 = 2)
		$exp = 1 / intval($e) - $exp;

//var_dump($e, $exp);

		$x = 1;
		$y = (($n * _pow($x, 1 - $e)) / $e) - ($x / $e) + $x;

		do
		{
			$x = $y;
			$y = (($n * _pow($x, 1 - $e)) / $e) - ($x / $e) + $x;
		} while ($x > $y);

		$result = $result * $x; // 4^2.5 = 4^2 * 4^0.5 = 16 * 2 = 32
	}
	
	return $result;
}

var_dump(pow(2, 0.123456));
var_dump(_pow(2, 0.123456));

?>


Output:
1
2
float(1.0893412745953)
float(1.0916778537052)


Create a new paste based on this one


Comments: