Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 21a74f5

Browse files
author
Andreu Correa
committed
Minor style & performance fixes
1 parent b3505f0 commit 21a74f5

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/Decimal.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
class Decimal
1818
{
19+
const DEFAULT_SCALE = 16;
1920
const CLASSIC_DECIMAL_NUMBER_REGEXP = '/^([+\-]?)0*(([1-9][0-9]*|[0-9])(\.[0-9]+)?)$/';
2021
const EXP_NOTATION_NUMBER_REGEXP = '/^ (?P<sign> [+\-]?) 0*(?P<mantissa> [0-9](?P<decimals> \.[0-9]+)?) [eE] (?P<expSign> [+\-]?)(?P<exp> \d+)$/x';
2122
const EXP_NUM_GROUPS_NUMBER_REGEXP = '/^ (?P<int> \d*) (?: \. (?P<dec> \d+) ) E (?P<sign>[\+\-]) (?P<exp>\d+) $/x';
@@ -91,16 +92,14 @@ public static function fromFloat(float $fltValue, int $scale = null, bool $remov
9192
throw new NaNInputError("fltValue can't be NaN");
9293
}
9394

94-
$defaultScale = 16;
95+
$defaultScale = self::DEFAULT_SCALE;
9596

9697
$strValue = (string) $fltValue;
9798
if (\preg_match(self::EXP_NUM_GROUPS_NUMBER_REGEXP, $strValue, $capture)) {
98-
if ($scale === null) {
99-
if ($capture['sign'] == '-') {
100-
$scale = $capture['exp'] + \strlen($capture['dec']);
101-
} else {
102-
$scale = $defaultScale;
103-
}
99+
if (null === $scale) {
100+
$scale = ('-' === $capture['sign'])
101+
? $capture['exp'] + \strlen($capture['dec'])
102+
: $defaultScale;
104103
}
105104
$strValue = \number_format($fltValue, $scale, '.', '');
106105
}
@@ -250,7 +249,7 @@ public function div(Decimal $b, int $scale = null): Decimal
250249
} elseif ($this->isZero()) {
251250
return DecimalConstants::Zero();
252251
} else {
253-
if ($scale !== null) {
252+
if (null !== $scale) {
254253
$divscale = $scale;
255254
} else {
256255
// $divscale is calculated in order to maintain a reasonable precision
@@ -323,7 +322,7 @@ public function pow(Decimal $b, int $scale = null): Decimal
323322
return DecimalConstants::One()->div(
324323
$this->pow($b->additiveInverse()), $scale
325324
);
326-
} elseif ($b->scale == 0) {
325+
} elseif ($b->scale === 0) {
327326
$pow_scale = $scale === null ?
328327
\max($this->scale, $b->scale) : \max($this->scale, $b->scale, $scale);
329328

@@ -435,7 +434,7 @@ public function equals(Decimal $b, int $scale = null): bool
435434
self::innerRound($this->value, $cmp_scale),
436435
self::innerRound($b->value, $cmp_scale),
437436
$cmp_scale
438-
) == 0
437+
) === 0
439438
);
440439
}
441440
}
@@ -878,10 +877,10 @@ public function exp(int $scale = null): Decimal
878877
return DecimalConstants::one();
879878
}
880879

881-
$scale = ($scale === null) ? \max(
880+
$scale = $scale ?? \max(
882881
$this->scale,
883-
(int)($this->isNegative() ? self::innerLog10($this->value, $this->scale, 0) : 16)
884-
) : $scale;
882+
(int)($this->isNegative() ? self::innerLog10($this->value, $this->scale, 0) : self::DEFAULT_SCALE)
883+
);
885884

886885
return self::factorialSerie(
887886
$this, DecimalConstants::one(), function ($i) { return DecimalConstants::one(); }, $scale
@@ -945,9 +944,9 @@ private static function powerSerie (Decimal $x, Decimal $firstTerm, int $scale):
945944
for ($i = 1; !$change->floor($scale + 2)->isZero(); $i++) {
946945
$xPowerN = $xPowerN->mul($x);
947946

948-
if ($i % 2 == 0) {
947+
if ($i % 2 === 0) {
949948
$factorN = DecimalConstants::zero();
950-
} elseif ($i == 1) {
949+
} elseif ($i === 1) {
951950
$factorN = DecimalConstants::one();
952951
} else {
953952
$incrementNum = Decimal::fromInteger($i - 2);

0 commit comments

Comments
 (0)