use Chippyash\Type\TypeFactory; use Chippyash\Type\RequiredType; use Chippyash\Type\Number\Rational\RationalTypeFactory; /** * Tuning * set tolerance for creating rationals from floats - sqrt() function will use it. * * Try setting this to the PHP int limit i.e. 1e-17. You will find that some * of the square roots cannot be computed because the limits of the mechanism * to convert floats to rational numbers busts the available precision and therefore * 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) {
/** * @runInSeparateProcess */ public function testSetDefaultFromFloatToleranceIsStatic() { RationalTypeFactory::setDefaultFromFloatTolerance(1.0E-5); $this->assertEquals('113/355', (string) RationalTypeFactory::fromFloat(M_1_PI)); RationalTypeFactory::setDefaultFromFloatTolerance(1.0E-15); $this->assertEquals('25510582/80143857', (string) RationalTypeFactory::fromFloat(M_1_PI)); }