Skip to content

Commit

Permalink
- new phpunit 8, 9
Browse files Browse the repository at this point in the history
- test providers now have strict types
- new for format trailing zeros
  • Loading branch information
krowinski committed Sep 10, 2020
1 parent 5cfd620 commit 1562a15
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 86 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"ext-bcmath": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.5"
"phpunit/phpunit": "^7.5|^8.0|^9.0"
},
"license": "MIT",
"authors": [
Expand Down
57 changes: 43 additions & 14 deletions src/BCMathExtended/BC.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function convertScientificNotationToString(string $number): string
$number = static::mul($pow, $regs[1], $scale);
}
// remove unnecessary 0 and dot from 0.000 is a 0
$number = static::trimTrailingZeroes($number);
$number = static::formatTrailingZeroes($number, $scale);
}

return static::checkNumber($number);
Expand Down Expand Up @@ -80,7 +80,7 @@ public static function pow(string $leftOperand, string $rightOperand, ?int $scal
$r = bcpow($leftOperand, $rightOperand, $scale);
}

return static::trimTrailingZeroes($r);
return static::formatTrailingZeroes($r, $scale);
}

protected static function checkIsFloat(string $number): bool
Expand Down Expand Up @@ -127,22 +127,51 @@ public static function sqrt(string $operand, ?int $scale = null): string
$r = bcsqrt($operand, $scale);
}

return static::trimTrailingZeroes($r);
return static::formatTrailingZeroes($r, $scale);
}

protected static function trimTrailingZeroes(string $number): string
protected static function formatTrailingZeroes(string $number, ?int $scale = null): string
{
if (!self::$trimTrailingZeroes) {
if (self::$trimTrailingZeroes) {
return static::trimTrailingZeroes($number);
}

// newer version of php correct add trailing zeros
if (PHP_VERSION_ID >= 70300) {
return $number;
}

// old one not so much..
return self::addTrailingZeroes($number, $scale);
}

protected static function trimTrailingZeroes(string $number): string
{
if (false !== strpos($number, '.')) {
$number = rtrim($number, '0');
}

return rtrim($number, '.') ?: '0';
}

protected static function addTrailingZeroes(string $number, ?int $scale): string
{
if (null === $scale) {
return $number;
}

$decimalLength = static::getDecimalsLengthFromNumber($number);
if ($scale === $decimalLength) {
return $number;
}

if (0 === $decimalLength) {
$number .= '.';
}

return str_pad($number, strlen($number) + ($scale - $decimalLength), '0', STR_PAD_RIGHT);
}

protected static function checkNumber(string $number): string
{
$number = str_replace('+', '', filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION));
Expand All @@ -164,14 +193,14 @@ public static function add(string $leftOperand, string $rightOperand, ?int $scal
$r = bcadd($leftOperand, $rightOperand, $scale);
}

return static::trimTrailingZeroes($r);
return static::formatTrailingZeroes($r, $scale);
}

public static function exp(string $arg): string
{
$scale = static::DEFAULT_SCALE;
$result = '1';
for ($i = 299; $i > 0; $i--) {
for ($i = 299; $i > 0; --$i) {
$result = static::add(static::mul(static::div($result, (string)$i, $scale), $arg, $scale), '1', $scale);
}

Expand All @@ -189,7 +218,7 @@ public static function mul(string $leftOperand, string $rightOperand, ?int $scal
$r = bcmul($leftOperand, $rightOperand, $scale);
}

return static::trimTrailingZeroes($r);
return static::formatTrailingZeroes($r, $scale);
}

public static function div(string $leftOperand, string $rightOperand, ?int $scale = null): string
Expand All @@ -207,7 +236,7 @@ public static function div(string $leftOperand, string $rightOperand, ?int $scal
$r = bcdiv($leftOperand, $rightOperand, $scale);
}

return static::trimTrailingZeroes($r);
return static::formatTrailingZeroes($r, $scale);
}

public static function log(string $arg): string
Expand All @@ -233,7 +262,7 @@ public static function log(string $arg): string
} else {
$res = static::sub($res, $sum, $scale);
}
$i++;
++$i;
} while (static::comp($sum, '0', $scale));

return static::add($res, $m, $scale);
Expand Down Expand Up @@ -266,7 +295,7 @@ public static function sub(string $leftOperand, string $rightOperand, ?int $scal
$r = bcsub($leftOperand, $rightOperand, $scale);
}

return static::trimTrailingZeroes($r);
return static::formatTrailingZeroes($r, $scale);
}

public static function setTrimTrailingZeroes(bool $flag): void
Expand Down Expand Up @@ -334,7 +363,7 @@ public static function powMod(
$r = bcpowmod($leftOperand, $rightOperand, $modulus, $scale);
}

return static::trimTrailingZeroes($r);
return static::formatTrailingZeroes($r, $scale);
}

public static function mod(string $leftOperand, string $modulus, ?int $scale = null): string
Expand All @@ -358,7 +387,7 @@ public static function mod(string $leftOperand, string $modulus, ?int $scale = n
);
}

return static::trimTrailingZeroes($r);
return static::formatTrailingZeroes($r, $scale);
}

public static function floor(string $number): string
Expand Down Expand Up @@ -548,7 +577,7 @@ protected static function recalculateNegative(string $number): string
{
$xor = str_repeat(static::dec2bin((string)(static::MAX_BASE - 1)), strlen($number));
$number ^= $xor;
for ($i = strlen($number) - 1; $i >= 0; $i--) {
for ($i = strlen($number) - 1; $i >= 0; --$i) {
$byte = ord($number[$i]);
if (++$byte !== static::MAX_BASE) {
$number[$i] = chr($byte);
Expand Down
Loading

0 comments on commit 1562a15

Please sign in to comment.