|
16 | 16 | */
|
17 | 17 | class Decimal
|
18 | 18 | {
|
| 19 | + const DEFAULT_SCALE = 16; |
19 | 20 | const CLASSIC_DECIMAL_NUMBER_REGEXP = '/^([+\-]?)0*(([1-9][0-9]*|[0-9])(\.[0-9]+)?)$/';
|
20 | 21 | const EXP_NOTATION_NUMBER_REGEXP = '/^ (?P<sign> [+\-]?) 0*(?P<mantissa> [0-9](?P<decimals> \.[0-9]+)?) [eE] (?P<expSign> [+\-]?)(?P<exp> \d+)$/x';
|
21 | 22 | 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
|
91 | 92 | throw new NaNInputError("fltValue can't be NaN");
|
92 | 93 | }
|
93 | 94 |
|
94 |
| - $defaultScale = 16; |
| 95 | + $defaultScale = self::DEFAULT_SCALE; |
95 | 96 |
|
96 | 97 | $strValue = (string) $fltValue;
|
97 | 98 | 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; |
104 | 103 | }
|
105 | 104 | $strValue = \number_format($fltValue, $scale, '.', '');
|
106 | 105 | }
|
@@ -250,7 +249,7 @@ public function div(Decimal $b, int $scale = null): Decimal
|
250 | 249 | } elseif ($this->isZero()) {
|
251 | 250 | return DecimalConstants::Zero();
|
252 | 251 | } else {
|
253 |
| - if ($scale !== null) { |
| 252 | + if (null !== $scale) { |
254 | 253 | $divscale = $scale;
|
255 | 254 | } else {
|
256 | 255 | // $divscale is calculated in order to maintain a reasonable precision
|
@@ -323,7 +322,7 @@ public function pow(Decimal $b, int $scale = null): Decimal
|
323 | 322 | return DecimalConstants::One()->div(
|
324 | 323 | $this->pow($b->additiveInverse()), $scale
|
325 | 324 | );
|
326 |
| - } elseif ($b->scale == 0) { |
| 325 | + } elseif ($b->scale === 0) { |
327 | 326 | $pow_scale = $scale === null ?
|
328 | 327 | \max($this->scale, $b->scale) : \max($this->scale, $b->scale, $scale);
|
329 | 328 |
|
@@ -435,7 +434,7 @@ public function equals(Decimal $b, int $scale = null): bool
|
435 | 434 | self::innerRound($this->value, $cmp_scale),
|
436 | 435 | self::innerRound($b->value, $cmp_scale),
|
437 | 436 | $cmp_scale
|
438 |
| - ) == 0 |
| 437 | + ) === 0 |
439 | 438 | );
|
440 | 439 | }
|
441 | 440 | }
|
@@ -878,10 +877,10 @@ public function exp(int $scale = null): Decimal
|
878 | 877 | return DecimalConstants::one();
|
879 | 878 | }
|
880 | 879 |
|
881 |
| - $scale = ($scale === null) ? \max( |
| 880 | + $scale = $scale ?? \max( |
882 | 881 | $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 | + ); |
885 | 884 |
|
886 | 885 | return self::factorialSerie(
|
887 | 886 | $this, DecimalConstants::one(), function ($i) { return DecimalConstants::one(); }, $scale
|
@@ -945,9 +944,9 @@ private static function powerSerie (Decimal $x, Decimal $firstTerm, int $scale):
|
945 | 944 | for ($i = 1; !$change->floor($scale + 2)->isZero(); $i++) {
|
946 | 945 | $xPowerN = $xPowerN->mul($x);
|
947 | 946 |
|
948 |
| - if ($i % 2 == 0) { |
| 947 | + if ($i % 2 === 0) { |
949 | 948 | $factorN = DecimalConstants::zero();
|
950 |
| - } elseif ($i == 1) { |
| 949 | + } elseif ($i === 1) { |
951 | 950 | $factorN = DecimalConstants::one();
|
952 | 951 | } else {
|
953 | 952 | $incrementNum = Decimal::fromInteger($i - 2);
|
|
0 commit comments