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

Commit bb64b59

Browse files
author
Andreu Correa
committed
Use guard clause in Decimal::pow
1 parent 4ebb197 commit bb64b59

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/Decimal.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,20 +340,16 @@ public function pow(Decimal $b, int $scale = null): Decimal
340340
$pow_scale
341341
);
342342
} else { // elseif ($this->isNegative())
343-
if ($b->isInteger()) {
344-
if (\preg_match('/^[+\-]?[0-9]*[02468](\.0+)?$/', $b->value, $captures) === 1) {
345-
// $b is an even number
346-
return $this->additiveInverse()->pow($b, $scale);
347-
} else {
348-
// $b is an odd number
349-
return $this->additiveInverse()->pow($b, $scale)->additiveInverse();
350-
}
343+
if (!$b->isInteger()) {
344+
throw new NotImplementedError(
345+
"Usually negative numbers can't be powered to non integer numbers. " .
346+
"The cases where is possible are not implemented."
347+
);
351348
}
352349

353-
throw new NotImplementedError(
354-
"Usually negative numbers can't be powered to non integer numbers. " .
355-
"The cases where is possible are not implemented."
356-
);
350+
return (\preg_match('/^[+\-]?[0-9]*[02468](\.0+)?$/', $b->value, $captures) === 1)
351+
? $this->additiveInverse()->pow($b, $scale) // $b is an even number
352+
: $this->additiveInverse()->pow($b, $scale)->additiveInverse(); // $b is an odd number
357353
}
358354
}
359355
}

0 commit comments

Comments
 (0)