public function testGetWithNoCalculatorSetDetectsCalculator()
 {
     $currentCalculator = Calculator::get();
     Calculator::set(null);
     $this->assertInstanceOf(Calculator::getNamespace(), Calculator::get());
     Calculator::set($currentCalculator);
 }
示例#2
0
文件: BigInteger.php 项目: brick/math
 /**
  * Returns a string representation of this number in the given base.
  *
  * @param int $base
  *
  * @return string
  *
  * @throws \InvalidArgumentException If the base is out of range.
  */
 public function toBase($base)
 {
     $base = (int) $base;
     if ($base === 10) {
         return $this->value;
     }
     if ($base < 2 || $base > 36) {
         throw new \InvalidArgumentException(sprintf('Base %d is out of range [2, 36]', $base));
     }
     $dictionary = '0123456789abcdefghijklmnopqrstuvwxyz';
     $calc = Calculator::get();
     $value = $this->value;
     $negative = $value[0] === '-';
     if ($negative) {
         $value = substr($value, 1);
     }
     $base = (string) $base;
     $result = '';
     while ($value !== '0') {
         list($value, $remainder) = $calc->divQR($value, $base);
         $remainder = (int) $remainder;
         $result .= $dictionary[$remainder];
     }
     if ($negative) {
         $result .= '-';
     }
     return strrev($result);
 }
示例#3
0
require 'vendor/autoload.php';
/**
 * @return Calculator
 */
function getCalculatorImplementation()
{
    switch ($calculator = getenv('CALCULATOR')) {
        case 'GMP':
            $calculator = new Calculator\GmpCalculator();
            break;
        case 'BCMath':
            $calculator = new Calculator\BcMathCalculator();
            break;
        case 'Native':
            $calculator = new Calculator\NativeCalculator();
            break;
        default:
            if ($calculator === false) {
                echo 'CALCULATOR environment variable not set!' . PHP_EOL;
            } else {
                echo 'Unknown calculator: ' . $calculator . PHP_EOL;
            }
            echo 'Example usage: CALCULATOR={calculator} vendor/bin/phpunit' . PHP_EOL;
            echo 'Available calculators: GMP, BCMath, Native' . PHP_EOL;
            exit(1);
    }
    echo 'Using ', get_class($calculator), PHP_EOL;
    return $calculator;
}
Calculator::set(getCalculatorImplementation());
示例#4
0
文件: BigDecimal.php 项目: brick/math
 /**
  * {@inheritdoc}
  */
 public function compareTo($that)
 {
     $that = BigNumber::of($that);
     if ($that instanceof BigInteger) {
         $that = $that->toBigDecimal();
     }
     if ($that instanceof BigDecimal) {
         $this->scaleValues($this, $that, $a, $b);
         return Calculator::get()->cmp($a, $b);
     }
     return -$that->compareTo($this);
 }