Ejemplo n.º 1
0
 public function elapse(Time $time)
 {
     foreach ($this->actions as $key => $action) {
         /** @var Time $timeLeft */
         $timeLeft = $action['time'];
         $method = $action['action'];
         $argument = $action['argument'];
         $timeLeft->toNative();
         $time->toNative();
         $timeLeft->subtract($time);
         if (BCProvider::compare($timeLeft->getValue(), 0) <= 0) {
             unset($this->actions[$key]);
             $executeTime = clone $timeLeft->add($time);
         } else {
             $executeTime = $time;
         }
         $reflectMethod = new \ReflectionMethod($this, $method);
         $params = $reflectMethod->getParameters();
         if (count($params) != 2) {
             throw new \Exception('Cannot invoke action because a parameter count mismatch.');
         }
         $args = [];
         foreach ($params as $pKey => $param) {
             if (strpos($param->getClass()->name, 'Time') !== false) {
                 $args[$pKey] = $executeTime;
             } else {
                 $args[$pKey] = $argument;
             }
         }
         $reflectMethod->invokeArgs($this, $args);
     }
     return $this;
 }
Ejemplo n.º 2
0
 public function getY($x)
 {
     $value = 0;
     foreach ($this->equationDefinition as $exponent => $coefficient) {
         $value = BCProvider::add($value, BCProvider::multiply($coefficient, BCProvider::exp($x, $exponent)));
     }
     return Numbers::make($this->numberType, $value);
 }
Ejemplo n.º 3
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();
     }
 }
Ejemplo n.º 4
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());
 }
Ejemplo n.º 5
0
 /**
  * @param NumberInterface|int|float|string $value
  * @return int
  */
 public function compare($value)
 {
     $value = Numbers::makeOrDont($this, $value, $this->getPrecision());
     if ($this->getBase() != 10) {
         $thisValue = $this->convertToBase(10)->getValue();
     } else {
         $thisValue = $this->getValue();
     }
     if ($value->getBase() != 10) {
         $thatValue = $value->convertToBase(10)->getValue();
     } else {
         $thatValue = $value->getValue();
     }
     $scale = $this->getPrecision() < $value->getPrecision() ? $this->getPrecision() : $value->getPrecision();
     return BCProvider::compare($thisValue, $thatValue, $scale);
 }