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

Commit

Permalink
Use guard clause in Decimal::pow
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreu Correa committed Apr 30, 2017
1 parent 4ebb197 commit bb64b59
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,20 +340,16 @@ public function pow(Decimal $b, int $scale = null): Decimal
$pow_scale
);
} else { // elseif ($this->isNegative())
if ($b->isInteger()) {
if (\preg_match('/^[+\-]?[0-9]*[02468](\.0+)?$/', $b->value, $captures) === 1) {
// $b is an even number
return $this->additiveInverse()->pow($b, $scale);
} else {
// $b is an odd number
return $this->additiveInverse()->pow($b, $scale)->additiveInverse();
}
if (!$b->isInteger()) {
throw new NotImplementedError(
"Usually negative numbers can't be powered to non integer numbers. " .
"The cases where is possible are not implemented."
);
}

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

0 comments on commit bb64b59

Please sign in to comment.