public function testDivisionByZero()
 {
     $x = Rational::parse('1/2');
     $y = Rational::parse('0');
     $this->setExpectedException(DivisionByZeroException::class);
     $z = Rational::div($x, $y);
 }
Exemple #2
0
 /**
  * string representation of a complex number
  *
  *
  * @param mixed $z (Complex, or parsable to Complex)
  * @retval string
  */
 public function __toString()
 {
     // TODO: use Number:: helper functions.
     $realAsRational = Rational::fromFloat($this->x);
     if ($realAsRational->q <= 100) {
         $real = "{$realAsRational}";
     } else {
         $real = sprintf('%f', $this->x);
     }
     $imagAsRational = Rational::fromFloat($this->y);
     if ($imagAsRational->q <= 100) {
         $imag = $imagAsRational->signed();
     } else {
         $imag = sprintf('%+f', $this->y);
     }
     if ($this->y == 0) {
         return $real;
     }
     if ($this->x == 0) {
         if ($this->y == 1) {
             return 'i';
         }
         if ($this->y == -1) {
             return '-i';
         }
         if ($imag[0] == '+') {
             $imag = substr($imag, 1);
         }
         return "{$imag}i";
     }
     if ($this->y == 1) {
         $imag = '+';
     }
     if ($this->y == -1) {
         $imag = '-';
     }
     return "{$real}{$imag}i";
 }