예제 #1
0
 /**
  * Note about precision: it uses the smaller of the two precisions (significant figures).
  *
  * @param $num
  * @return mixed
  */
 public function divide($num)
 {
     $num = Numbers::makeOrDont($this, $num, $this->getPrecision());
     $oldBase = $this->convertForModification();
     $numOldBase = $num->convertForModification();
     $precision = $this->getPrecision() > $num->getPrecision() ? $num->getPrecision() : $this->getPrecision();
     $value = BCProvider::divide($this->getValue(), $num->getValue(), $precision);
     $this->convertFromModification($oldBase);
     $num->convertFromModification($numOldBase);
     return $this->setValue($value);
 }
예제 #2
0
 /**
  * Generates a random number in the provided range, where all possible values are equally likely. (Even distribution.)
  *
  * NOTE: If $max is more than PHP_INT_MAX or $min is less than PHP_INT_MIN, no additional entropy will be gained for
  * the random number, and the distribution will become less evenly distributed across all possible values due to
  * rounding.
  *
  * @param $min
  * @param $max
  * @return NumberInterface
  */
 public function random($min = 0, $max = PHP_INT_MAX)
 {
     $min = Numbers::makeOrDont(Numbers::IMMUTABLE, $min);
     $max = Numbers::makeOrDont(Numbers::IMMUTABLE, $max);
     $difference = new ImmutableNumber(BCProvider::add($max->absValue(), $min->absValue()));
     $randFactory = new Factory();
     if ($max->compare(PHP_INT_MAX) != 1 && $min->compare(PHP_INT_MIN) != -1 && $difference->compare(PHP_INT_MAX) != 1) {
         $x = $randFactory->getMediumStrengthGenerator()->generateInt($min, $max);
         return Numbers::makeFromBase10($this->numberType, $x, null, $this->contextBase);
     } else {
         $x = $randFactory->getMediumStrengthGenerator()->generateInt();
         $fraction = BCProvider::divide($x, PHP_INT_MAX);
         $addedValue = BCProvider::multiply($fraction, $difference->getValue());
         $randVal = Numbers::makeFromBase10($this->numberType, BCProvider::add($min->getValue(), $addedValue), null, $this->contextBase);
         return $randVal->round();
     }
 }
예제 #3
0
 public function PDFByValue($value)
 {
     $value = Numbers::makeOrDont($this->numberType, $value);
     return Numbers::make($this->numberType, BCProvider::multiply(BCProvider::divide(1, BCProvider::multiply($this->standardDev->getValue(), BCProvider::squareRoot(M_2_PI))), BCProvider::exp(M_E, BCProvider::multiply(-1, BCProvider::divide(BCProvider::exp(BCProvider::subtract($value->getValue(), $this->mean->getValue()), 2), BCProvider::multiply(2, $this->variance->getValue()))))), $value->getPrecision(), $value->getBase());
 }