/** * @param string $left_operand * @param string $right_operand * @return string */ public function sqrt($operand) { $result = gmp_sqrt($operand); return gmp_strval($result); }
<?php var_dump(gmp_strval(gmp_sqrt(-2))); var_dump(gmp_strval(gmp_sqrt("-2"))); var_dump(gmp_strval(gmp_sqrt("0"))); var_dump(gmp_strval(gmp_sqrt("2"))); var_dump(gmp_strval(gmp_sqrt("144"))); $n = gmp_init(0); var_dump(gmp_strval(gmp_sqrt($n))); $n = gmp_init(-144); var_dump(gmp_strval(gmp_sqrt($n))); $n = gmp_init(777); var_dump(gmp_strval(gmp_sqrt($n))); var_dump(gmp_sqrt($n, 1)); var_dump(gmp_sqrt()); var_dump(gmp_sqrt(array())); echo "Done\n";
/** * Returns the (integer) square root of a Math_Integer number * * @param object Math_Integer $int1 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &sqrt(&$int1) { /*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) { return $err; } switch (MATH_INTLIB) { /*{{{*/ case 'gmp': $tmp = gmp_strval(gmp_sqrt($int1->getValue())); break; case 'bcmath': $tmp = bcsqrt(-1, $int1->getValue()); break; case 'std': $tmp = intval(sqrt($int1->getValue())); break; } /*}}}*/ return new Math_Integer($tmp); }
function bcsqrt($x) { return gmp_strval(gmp_sqrt($x)); }
$a = gmp_init("0xff"); echo gmp_strval($a), ' -> 0b', gmp_strval($a, 2), "\n"; gmp_setbit($a, 0, false); // clear bit at index 0 echo gmp_strval($a), ' -> 0b', gmp_strval($a, 2), "\n"; // gmp_sign echo gmp_sign("500") . "\n"; // positive echo gmp_sign("-500") . "\n"; // negative echo gmp_sign("0") . "\n"; // zero // gmp_sqrt $sqrt1 = gmp_sqrt("9"); $sqrt2 = gmp_sqrt("7"); $sqrt3 = gmp_sqrt("1524157875019052100"); echo gmp_strval($sqrt1) . "\n"; echo gmp_strval($sqrt2) . "\n"; echo gmp_strval($sqrt3) . "\n"; // gmp_sqrtrem list($sqrt1, $sqrt1rem) = gmp_sqrtrem("9"); list($sqrt2, $sqrt2rem) = gmp_sqrtrem("7"); list($sqrt3, $sqrt3rem) = gmp_sqrtrem("1048576"); echo gmp_strval($sqrt1) . ", " . gmp_strval($sqrt1rem) . "\n"; echo gmp_strval($sqrt2) . ", " . gmp_strval($sqrt2rem) . "\n"; echo gmp_strval($sqrt3) . ", " . gmp_strval($sqrt3rem) . "\n"; // gmp_strval $a = gmp_init("0x41682179fbf5"); printf("Decimal: %s, 36-based: %s" . PHP_EOL, gmp_strval($a), gmp_strval($a, 36)); // gmp_sub $sub = gmp_sub("281474976710656", "4294967296");
public function Sqr($left) { return $this->string(gmp_sqrt($left)); }
/** * Get the square route * * @access public * @return self */ public function sqrt() { $result = gmp_sqrt($this->getRawValue()); return static::factory($result); }
/** * @param string $left_operand * @param string $right_operand * @return string */ public function sqrt($operand) { return gmp_strval(gmp_sqrt($operand)); }
/** * Return all prime factors of this number * * The keys (prime factors) will be strings * The values (exponents will be integers * * Adapted from http://www.thatsgeeky.com/2011/03/prime-factoring-with-php/ * * @return array [primeFactor => exponent,...] */ public function primeFactors() { $number = $this->cloneValue(); $divisor = 2; $zero = gmp_init(0); $one = gmp_init(1); $dmax = gmp_sqrt($number); $factors = array(); $sieve = array_fill(1, intval(gmp_strval($dmax)), 1); do { $rFlag = false; while (gmp_cmp(gmp_mod($number, $divisor), $zero) == 0) { $factors[$divisor] = isset($factors[$divisor]) ? $factors[$divisor] + 1 : 1; $number = gmp_div_q($number, $divisor); $rFlag = true; } if ($rFlag) { $dmax = gmp_sqrt($number); } if (gmp_cmp($number, $one) > 0) { for ($i = $divisor; gmp_cmp($i, $dmax) <= 0; $i += $divisor) { $sieve[$i] = 0; } do { $divisor++; } while (gmp_cmp($divisor, $dmax) < 0 && $sieve[$divisor] != 1); if (gmp_cmp($divisor, $dmax) > 0) { $key = gmp_strval($number); $factors[$key] = isset($factors[$key]) ? $factors[$key] + 1 : 1; } } } while (gmp_cmp($number, $one) > 0 && gmp_cmp($divisor, $dmax) <= 0); return $factors; }
/** * Calculates & returns the integer portion of the square root. * * @param string $a The first number. * @return string */ public function sqrt($a) { return gmp_strval(gmp_sqrt($a)); }