Exemple #1
0
 * we get into an overflow situation.
 *
 * Setting tolerance to a lower number, say 1e-6, will compute faster but at the
 * expense of accuracy
 */
RationalTypeFactory::setDefaultFromFloatTolerance(1.0E-15);
/**
 * Set the required number type.  System will automatically use GMP if
 * it is available.  You can force it to use native PHP thus:
 */
RequiredType::getInstance()->set(RequiredType::TYPE_NATIVE);
//now create 10000 numbers for the test
//try playing with this figure to see the results
$numbers = [];
for ($x = 1; $x < 10001; $x++) {
    $numbers[$x] = TypeFactory::create('int', $x);
}
//create primes
$primes = [];
$start = microtime(true);
foreach ($numbers as $key => $number) {
    $primes[$key] = $number->primeFactors();
}
$end = microtime(true);
$time = $end - $start;
echo "{$time} secs.\n";
echo "And the results were:\n";
foreach ($primes as $key => $res) {
    $p = array_keys($res);
    $e = array_values($res);
    $factors = "{$key}=>";
 /**
  * Return number as a FloatType number.
  *
  * @return \Chippyash\Type\Number\FloatType
  * @throws NotRealComplexException
  */
 public function asFloatType()
 {
     if ($this->isReal()) {
         return TypeFactory::create('float', $this->value['real']->get());
     } else {
         throw new NotRealComplexException();
     }
 }
        } else {
            echo "{$op}({$tA}({$p1})) = {$tR}({$res})" . PHP_EOL;
        }
    } catch (InvalidTypeException $e) {
        $msg = $e->getMessage();
        echo "{$tA}({$p1}) {$op} {$tB}({$p2}) is invalid: {$msg}" . PHP_EOL;
    }
}
$iphp = 19;
$fphp = 13.27;
$i = F::create('int', 6);
$w = F::create('whole', 21);
$n = F::create('natural', 13);
$f = F::create('float', 2.26);
$r = F::Create('rational', '2/3');
$c = F::create('complex', '2+6i');
$items = [$iphp, $i, $w, $n, $fphp, $f, $r, $c];
echo "Library supported arithmetic operations and return types\n";
echo "Non complex numbers\n";
echo "Addition\n\n";
foreach ($items as $a) {
    foreach ($items as $b) {
        display('add', $a, $b);
    }
}
echo "\nSubtraction\n\n";
foreach ($items as $a) {
    foreach ($items as $b) {
        display('sub', $a, $b);
    }
}
 /**
  * @param RationalType $radius
  * @param RationalType $sin
  *
  * @return array
  *
  * @throws InvalidTypeException
  */
 private static function getImaginaryPartsFromRadiusAndSin(RationalType $radius, RationalType $sin)
 {
     return array(TypeFactory::create('int', $radius->numerator()->get() * $sin->numerator()->get()), TypeFactory::create('int', $radius->denominator()->get() * $sin->denominator()->get()));
 }
 public function testSetNumberTypeToDefaultWillSetGmpIfAvailable()
 {
     RequiredType::getInstance()->set(RequiredType::TYPE_DEFAULT);
     $this->assertInstanceOf('Chippyash\\Type\\Number\\Rational\\GMPRationalType', TypeFactory::create('rational', 2));
 }
 /**
  * @requires extension gmp
  * @runInSeparateProcess
  */
 public function testCreatingFloatsViaTypeFactoryUnderGmpWillReturnGMPRationalType()
 {
     RequiredType::getInstance()->set(RequiredType::TYPE_GMP);
     $this->assertInstanceOf('Chippyash\\Type\\Number\\Rational\\GMPRationalType', TypeFactory::create('float', 2 / 3));
 }