/** * Compute standard deviation. * * @param array $a The array of data to find the standard deviation for. * Note that all values of the array will be cast to float. * @param bool $is_sample [Optional] Indicates if $a represents a sample of the * population (otherwise its the population); Defaults to false. * @return string|bool The standard deviation or false on error. */ function stddev(array $a, $is_sample = false) { if (math_count($a) < 2) { trigger_error("The array has too few elements", E_USER_NOTICE); return false; } return bcsqrt(variance($a, $is_sample)); }
/** * Generate a valid prime based on which php is used. Slower than the Prime generator. * @return \Generator */ public static function generator() { $primes = ['2']; $currentNumber = '2'; (yield '2'); while (true) { ++$currentNumber; $unitNumber = (int) substr($currentNumber, -1); if (($currentNumber & 1) === 0) { continue; } $squareRoot = bcsqrt($currentNumber); $foundPrimeDivisor = false; foreach ($primes as $prime) { if (bccomp($prime, $squareRoot) === 1) { break; } if (bcmod($currentNumber, $prime) === '0') { $foundPrimeDivisor = true; break; } } if (!$foundPrimeDivisor) { $primes[] = $currentNumber; (yield $currentNumber); } } }
protected function _bcpi($precision) { $num = 0; $k = 0; bcscale($precision + 3); $limit = ($precision + 3) / 14; while ($k < $limit) { $num = bcadd($num, bcdiv(bcmul(bcadd('13591409', bcmul('545140134', $k)), bcmul(bcpow(-1, $k), $this->_bcfact(6 * $k))), bcmul(bcmul(bcpow('640320', 3 * $k + 1), bcsqrt('640320')), bcmul($this->_bcfact(3 * $k), bcpow($this->_bcfact($k), 3))))); ++$k; } return bcdiv(1, bcmul(12, $num), $precision); }
public function make($value) { $n = $value; $factors = array(); for ($divisor = 2; $n > 1 && $divisor <= bcsqrt($n); $divisor++) { for (; bcmod($n, $divisor) == 0; $n = bcdiv($n, $divisor)) { $factors[] = $divisor; } } if ($n > 1) { $factors[] = $n; } return $factors; }
protected function execute(InputInterface $input, OutputInterface $output) { $items = explode('.', $input->getArgument('item')); $pointName = $items[0]; $x1 = $items[1]; $y1 = $items[2]; $x2 = $items[3]; $y2 = $items[4]; // Used for mocking heavy execution. $sum = 0; for ($i = 1; $i <= 30000000; $i++) { $sum += $i; } $distance = bcsqrt(pow($x2 - $x1, 2) + pow($y2 - $y1, 2)); $data = sprintf('Point %s: %s', $pointName, (string) $distance); file_put_contents(__DIR__ . '/../../../output/Point' . $pointName, print_r($data, 1), FILE_APPEND); }
/** * Get decimal expansion of PI using Gauss-Lagendre algorithm. * * @link https://github.com/natmchugh/pi/blob/master/gauss-legendre.php * @link http://en.wikipedia.org/wiki/Calculate_pi#Modern_algorithms * @param $precision * @return string */ public static function calcPi($precision) { $limit = ceil(log($precision) / log(2)) - 1; bcscale($precision + 6); $a = 1; $b = bcdiv(1, bcsqrt(2)); $t = 1 / 4; $p = 1; for ($n = 0; $n < $limit; $n++) { $x = bcdiv(bcadd($a, $b), 2); $y = bcsqrt(bcmul($a, $b)); $t = bcsub($t, bcmul($p, bcpow(bcsub($a, $x), 2))); $a = $x; $b = $y; $p = bcmul(2, $p); } return bcdiv(bcpow(bcadd($a, $b), 2), bcmul(4, $t), $precision); }
/** * @param array $map */ public function filter(array $map) { $this->size = bcsqrt(count($map)); $intermediateArray = $filteredArray = []; foreach (range(0, $this->size) as $x) { foreach (range(0, $this->size) as $y) { $index = $x + $y * $this->size; $intermediateArray[$index] = $this->computeXFilteredValue($map, $x, $y); } } foreach (range(0, $this->size) as $x) { foreach (range(0, $this->size) as $y) { $index = $x + $y * $this->size; $filteredArray[$index] = $this->computeYFilteredValue($intermediateArray, $x, $y); } } return $filteredArray; }
function bcpi($precision) { $limit = ceil(log($precision) / log(2)) - 1; bcscale($precision + 6); $a = 1; $b = bcdiv(1, bcsqrt(2)); $t = 1 / 4; $p = 1; while ($n < $limit) { $x = bcdiv(bcadd($a, $b), 2); $y = bcsqrt(bcmul($a, $b)); $t = bcsub($t, bcmul($p, bcpow(bcsub($a, $x), 2))); $a = $x; $b = $y; $p = bcmul(2, $p); ++$n; } return bcdiv(bcpow(bcadd($a, $b), 2), bcmul(4, $t), $precision); }
function next_prime() { while (true) { $this->count = bcadd($this->count, '1'); $sqrt = bcsqrt($this->count); $numfactors = 0; for ($factor = 1; $factor < $sqrt; $factor++) { $remainder = bcmod($this->count, $factor); if ($remainder == 0) { $numfactors++; } if ($numfactors == 2) { break; // Not prime } } if ($numfactors < 2) { return $this->count; } } }
public static function _sqrt($operand, $scale = 50) { if (function_exists("bcsqrt")) { return bcsqrt(ilMath::exp2dec($operand), $scale); } else { $res = sqrt($operand); if (is_numeric($scale)) { $res = round($res, $scale); } return $res; } }
/** * Set a new value * * @param $value mixed - Value as string, integer, real or float * @param $type type - OPTIONAL a Zend_Measure_Number Type * @param $locale locale - OPTIONAL a Zend_Locale Type * @throws Zend_Measure_Exception */ public function setValue($value, $type, $locale = false) { if (empty($locale)) { $locale = $this->_Locale; } if (empty(self::$_UNITS[$type])) { throw Zend::exception('Zend_Measure_Exception', 'unknown type of number:' . $type); } switch ($type) { case 'Number::BINARY': preg_match('/[01]+/', $value, $ergebnis); $value = $ergebnis[0]; break; case 'Number::TERNARY': preg_match('/[012]+/', $value, $ergebnis); $value = $ergebnis[0]; break; case 'Number::QUATERNARY': preg_match('/[0123]+/', $value, $ergebnis); $value = $ergebnis[0]; break; case 'Number::QUINARY': preg_match('/[01234]+/', $value, $ergebnis); $value = $ergebnis[0]; break; case 'Number::SENARY': preg_match('/[012345]+/', $value, $ergebnis); $value = $ergebnis[0]; break; case 'Number::SEPTENARY': preg_match('/[0123456]+/', $value, $ergebnis); $value = $ergebnis[0]; break; case 'Number::OCTAL': preg_match('/[01234567]+/', $value, $ergebnis); $value = $ergebnis[0]; break; case 'Number::NONARY': preg_match('/[012345678]+/', $value, $ergebnis); $value = $ergebnis[0]; break; case 'Number::DUODECIMAL': preg_match('/[0123456789AB]+/', strtoupper($value), $ergebnis); $value = $ergebnis[0]; break; case 'Number::HEXADECIMAL': preg_match('/[0123456789ABCDEF]+/', strtoupper($value), $ergebnis); $value = $ergebnis[0]; break; case 'Number::ROMAN': preg_match('/[IVXLCDM_]+/', strtoupper($value), $ergebnis); $value = $ergebnis[0]; break; default: try { $value = Zend_Locale_Format::getInteger($value, $locale); } catch (Exception $e) { throw Zend::exception('Zend_Measure_Exception', $e->getMessage()); } if (bccomp($value, 0) < 0) { $value = bcsqrt(bcpow($value, 2)); } break; } parent::setValue($value, $type, $locale); parent::setType($type); }
/** * 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); }
private static function is_prime($number){ $limit = round(bcsqrt($number)); $counter = 2; while ($counter <= $limit){ if (bcmod($number, $counter) == 0){ return true; } $counter ++; } return false; }
/** * Provides the approximate distance in feet between two points. * * @param $lat1 * @param $lat2 * @param $long1 * @param $long2 * @return float */ function distance_in_feet($lat1, $lat2, $long1, $long2, $scale = 6) { bcscale($scale); $r = 20902231; //feet $rlat1 = deg2rad($lat1); $rlat2 = deg2rad($lat2); $rdlat = deg2rad($lat2 - $lat1); $rdlong = deg2rad($long2 - $long1); $a = sin(bcdiv($rdlat, 2)) * sin(bcdiv($rdlat, 2)) + cos($rlat1) * cos($rlat2) * sin(bcdiv($rdlong, 2)) * sin(bcdiv($rdlong, 2)); $c = bcmul(2, atan2(bcsqrt($a), bcsqrt(1 - $a))); return (double) ($r * $c); }
/** * Returns the square root of this number * * @param integer $scale The number of places after the decimal - overrides the scale for this number * @return fNumber The square root */ public function sqrt($scale = NULL) { $scale = $this->fixScale($scale); if ($this->sign() == -1) { throw new fProgrammerException('This number, %s, can not have the square root calculated since it is a negative number', $this->value); } if (function_exists('bcsqrt')) { $value = bcsqrt($this->value, $scale); return new fNumber($value, $scale); } // Pure PHP implementation $parts = explode('.', $this->value); $integer = substr($parts[0], 1); $fraction = isset($parts[1]) ? $parts[1] : ''; if (strlen($integer) % 2 == 1) { $integer = '0' . $integer; } if (strlen($fraction) % 2 == 1) { $fraction .= '0'; } $after_decimal = strlen($fraction) / 2; $number = $integer . $fraction; $i = 0; $remainder = '0'; $p = '0'; $len = strlen($number); $len += $scale * 2 - $after_decimal > 0 ? $scale * 2 - $after_decimal : 0; while ($i < $len) { if ($i < strlen($number)) { $c = substr($number, $i, 2); } else { $c = '00'; $after_decimal++; } if (!self::isZero($remainder)) { $c = $remainder . $c; } $x = -1; $p2 = self::performMul($p, '2'); do { $x++; } while (self::cmp(self::performMul($p2 . $x, $x), $c) <= 0); $x--; $y = self::performMul($p2 . $x, $x); $p = $p ? $p . $x : $x; $remainder = self::performSub($c, $y); $i += 2; } if (strlen($p) <= $after_decimal) { $p = $p[0] . str_pad(substr($p, 1), $after_decimal + 1, '0', STR_PAD_LEFT); } $integer = substr($p, 0, strlen($p) - $after_decimal); $fraction = substr($p, strlen($p) - $after_decimal); $fraction = strlen($fraction) ? '.' . $fraction : ''; $p = $integer . $fraction; $p = self::setScale($p, $scale); $p = self::stripLeadingZeroes($p); return new fNumber($p); }
public static function sqrt($val, $scale = null) { $scale = static::getScale($scale); return bcsqrt($val, $scale); }
/** * Performs a square root operation with the given number. * * @return BigNumber */ public function sqrt() { $newValue = bcsqrt($this->getValue(), $this->getScale()); return $this->assignValue($newValue); }
<?php echo bcsqrt('-9');
/** * Function which performs calculation. * @param string|integer|float|boolean $number1 See bcCalc description * @param string $action See bcCalc description * @param string|integer|float|boolean $number2 See bcCalc description * @param boolean $round See bcCalc description * @param integer $decimals See bcCalc description * @param integer $precision See bcCalc description * @return integer|float|boolean */ private static function performCalc($number1, $action, $number2, $round, $decimals, $precision) { $result = null; $compare = false; switch ($action) { case '+': $result = self::$blnBcmath ? bcadd($number1, $number2, $precision) : $number1 + $number2; break; case '-': $result = self::$blnBcmath ? bcsub($number1, $number2, $precision) : $number1 - $number2; break; case '*': $result = self::$blnBcmath ? bcmul($number1, $number2, $precision) : $number1 * $number2; break; case 'sqrt': $result = self::$blnBcmath ? bcsqrt($number1, $precision) : sqrt($number1); break; case '/': if ($number2 > 0) { if (self::$blnBcmath) { $result = bcdiv($number1, $number2, $precision); // string, or NULL if right_operand is 0 } else { if ($number2 != 0) { $result = $number1 / $number2; } } } if (!isset($result)) { $result = 0; } break; case '%': if (self::$blnBcmath) { $result = bcmod($number1, $number2); // string, or NULL if modulus is 0. } else { if ($number2 != 0) { $result = $number1 % $number2; } } if (!isset($result)) { $result = 0; } break; case '=': $compare = true; if (self::$blnBcmath) { $result = bccomp($number1, $number2, $precision); // returns int 0, 1 or -1 } else { $result = $number1 == $number2 ? 0 : ($number1 > $number2 ? 1 : -1); } break; } if (isset($result)) { if ($compare === false) { if ($round === true) { $result = round(floatval($result), $decimals); if ($decimals === 0) { $result = (int) $result; } } else { $result = intval($result) == $result ? intval($result) : floatval($result); } } return $result; } return false; }
/** * Calculates the square root of the operand * * @param string $operand The operand, as a string. * @param integer $precision The optional scale parameter is used to set the number of digits after the decimal place * @return string Return the square root of the operand. */ public static function sqrt($operand, $precision = null) { return self::clean(is_null($precision) ? bcsqrt($operand) : bcsqrt($operand, $precision)); }
private function sqrt($a) { return bcsqrt($a, 5); }
/** * @return $this */ public function sqrt() { $this->value = bcsqrt($this->value, $this->scale); return $this; }
/** * Calculates & returns the integer portion of the square root. * * @param string $a The first number. * @return string */ public function sqrt($a) { return bcsqrt($this->bcNormalize($a)); }
/** * Auxiliar method. Computes $base^((1/2)^$index) * * @param string $base * @param integer $index * @param integer $out_scale * @return string */ private static function compute2NRoot($base, $index, $out_scale) { $result = $base; for ($i = 0; $i < $index; $i++) { $result = bcsqrt($result, ($out_scale + 1) * ($index - $i) + 1); } return self::innerRound($result, $out_scale); }
<?php echo bcsqrt();
/** * Calculates the square root of this money. * * @return Money */ public function squareRoot() { $amount = bcsqrt($this->amount, self::BCSCALE); return self::valueOf($amount, $this->currency); }
/** * @param string $left_operand * @param string $right_operand * @return string */ public function sqrt($operand) { return bcsqrt($operand); }
/** * Returns bcscale() setted value * * @return int */ public function getBcScale() { $sqrt = bcsqrt('2'); return strlen(substr($sqrt, strpos($sqrt, '.') + 1)); }
/** * BCSqrt - fixes a problem of BCMath and exponential numbers * * @param string $op1 * @param integer $scale * @return string */ public static function Sqrt($op1, $scale = null) { $op1 = self::exponent($op1, $scale); return bcsqrt($op1, $scale); }
<?php echo bcsqrt("0");