Skip to content

Commit

Permalink
SmartNumber: Fix support for big numbers converted to fraction and fi…
Browse files Browse the repository at this point in the history
…x divided by zero + lowest code complexity by elseif.
  • Loading branch information
janbarasek authored Oct 17, 2019
1 parent 0c2855c commit c278a1c
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/Entity/SmartNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,13 @@ private function setFractionHelper(string $float, float $tolerance = 1.e-8): arr
$denominator = $a * $denominator + $subDenominator;
$subDenominator = $aux;
$b -= $a;
} while (abs($float - $numerator / $denominator) > $float * $tolerance);
} while ($denominator > 0 && abs($float - $numerator / $denominator) > $float * $tolerance);
} elseif (preg_match('/^(.*)\.(.*)$/', $float, $floatParser)) {
$numerator = ltrim($floatParser[1] . $floatParser[2], '0');
$denominator = '1' . str_repeat('0', \strlen($floatParser[2]));
} else {
if (preg_match('/^(.*)\.(.*)$/', $float, $floatParser)) {
$numerator = ltrim($floatParser[1] . $floatParser[2], '0');
$denominator = '1' . str_repeat('0', \strlen($floatParser[2]));
} else {
$numerator = str_replace('.', '', $float);
$denominator = '1';
}
$numerator = str_replace('.', '', $float);
$denominator = '1';
}

$short = $this->shortFractionHelper(number_format($numerator, 0, '.', ''), number_format($denominator, 0, '.', ''));
Expand Down

0 comments on commit c278a1c

Please sign in to comment.