Skip to content

Commit

Permalink
Reduce complexity to acceptable levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Riimu committed Jan 26, 2015
1 parent b8627b2 commit 87aace8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/BaseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function convert ($number)
$fractions = '';
$sign = '';

if (isset($integer[0]) && in_array($integer[0], ['+', '-'])) {
if (in_array(substr($integer, 0, 1), ['+', '-'], true)) {
$sign = $integer[0];
$integer = substr($integer, 1);
}
Expand Down
11 changes: 7 additions & 4 deletions src/DecimalConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public function convertFractions(array $number)
$target = gmp_init($this->target->getRadix());
$dividend = $this->toDecimal($this->source->getValues($number));
$divisor = $this->toDecimal([1] + array_fill(1, max(count($number), 1), 0));
$digits = $this->precision > 0
? $this->precision : $this->countDigits(count($number)) + abs($this->precision);
$digits = $this->getFractionDigitCount(count($number));
$zero = gmp_init('0');
$result = [];

Expand Down Expand Up @@ -124,8 +123,12 @@ private function toBase($decimal)
* @param integer $count Number of digits in the original number
* @return integer Number of digits required in the target base
*/
private function countDigits($count)
private function getFractionDigitCount($count)
{
if ($this->precision > 0) {
return $this->precision;
}

$target = gmp_init($this->target->getRadix());
$maxFraction = gmp_pow(gmp_init($this->source->getRadix()), $count);
$digits = 1;
Expand All @@ -134,6 +137,6 @@ private function countDigits($count)
$digits++;
}

return $digits;
return $digits + abs($this->precision);
}
}

0 comments on commit 87aace8

Please sign in to comment.