Пример #1
0
 public function testNoZeroZero()
 {
     $zero = DecimalConstants::Zero();
     $one = DecimalConstants::One();
     $nTwo = Decimal::fromInteger(-2);
     $pTwo = Decimal::fromInteger(2);
     $this->assertTrue($nTwo->pow($zero)->equals($one));
     $this->assertTrue($pTwo->pow($zero)->equals($one));
 }
Пример #2
0
 /**
  * Returns the square root of this object
  * @param  integer $scale
  * @return Decimal
  */
 public function sqrt($scale = null)
 {
     if ($this->isNegative()) {
         throw new \DomainException("Decimal can't handle square roots of negative numbers (it's only for real numbers).");
     } elseif ($this->isZero()) {
         return DecimalConstants::Zero();
     }
     $sqrt_scale = $scale !== null ? $scale : $this->scale;
     return self::fromString(bcsqrt($this->value, $sqrt_scale + 1), $sqrt_scale);
 }
 /**
  * @expectedException \DomainException
  * @expectedExceptionMessage Infinite elevated to zero is undefined.
  */
 public function testNegativeInfiniteZeroPower()
 {
     InfiniteDecimal::getNegativeInfinite()->pow(DecimalConstants::Zero());
 }
 /**
  * Powers this value to $b
  *
  * @param  Decimal  $b      exponent
  * @param  integer  $scale
  * @return Decimal
  */
 public function pow(Decimal $b, $scale = null)
 {
     if ($b->isPositive()) {
         if ($this->isPositive()) {
             return $this;
         }
         // if ($this->isNegative())
         if ($b->isInfinite()) {
             throw new \DomainException("Negative infinite elevated to infinite is undefined.");
         }
         if ($b->isInteger()) {
             if (preg_match('/^[+\\-]?[0-9]*[02468](\\.0+)?$/', $b->value, $captures) === 1) {
                 // $b is an even number
                 return self::$pInf;
             } else {
                 // $b is an odd number
                 return $this;
                 // Negative Infinite
             }
         }
         throw new NotImplementedException("See issues #21, #22, #23 and #24 on Github.");
     } else {
         if ($b->isNegative()) {
             return DecimalConstants::Zero();
         } else {
             if ($b->isZero()) {
                 throw new \DomainException("Infinite elevated to zero is undefined.");
             }
         }
     }
 }