本文整理汇总了PHP中bcsqrt函数的典型用法代码示例。如果您正苦于以下问题:PHP bcsqrt函数的具体用法?PHP bcsqrt怎么用?PHP bcsqrt使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了bcsqrt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: stddev
/**
* 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));
}
示例2: generator
/**
* 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);
}
}
}
示例3: _bcpi
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);
}
示例4: make
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;
}
示例5: execute
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);
}
示例6: calcPi
/**
* 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);
}
示例7: filter
/**
* @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;
}
示例8: bcpi
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);
}
示例9: next_prime
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;
}
}
}
示例10: _sqrt
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;
}
}
示例11: setValue
/**
* 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);
}
示例12: switch
/**
* 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);
}
示例13: is_prime
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;
}
示例14: distance_in_feet
/**
* 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);
}
示例15: sqrt
/**
* 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);
}