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

Commit a8b49f4

Browse files
committed
Simplified code in Decimal
1 parent 21bfd0e commit a8b49f4

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/Decimal.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public function add(Decimal $b, $scale = null)
272272
if ($this->isInfinite()) {
273273
if (!$b->isInfinite()) {
274274
return $this;
275-
} elseif ($this->isPositive() && $b->isPositive() || $this->isNegative() && $b->isNegative()) {
275+
} elseif ($this->hasSameSign($b)) {
276276
return $this;
277277
} else { // elseif ($this->isPositive() && $b->isNegative || $this->isNegative() && $b->isPositive()) {
278278
throw new \DomainException("Infinite numbers with opposite signs can't be added");
@@ -325,12 +325,14 @@ public function mul(Decimal $b, $scale = null)
325325
{
326326
self::paramsValidation($b, $scale);
327327

328-
if ($this->isZero() && $b->isInfinite() || $this->isInfinite() && $b->isZero()) {
328+
if ($this->isInfinite() || $b->isZero()) {
329+
return $b->mul($this);
330+
} elseif ($this->isZero() && $b->isInfinite()) {
329331
throw new \DomainException("Zero multiplied by infinite is not allowed.");
330-
} elseif ($this->isZero() && !$b->isInfinite() || !$this->isInfinite() && $b->isZero()) {
332+
} elseif ($this->isZero() && !$b->isInfinite()) {
331333
return Decimal::fromInteger(0, $scale);
332-
} elseif ($this->isInfinite() || $b->isInfinite()) {
333-
if ($this->isPositive() && $b->isPositive() || $this->isNegative() && $b->isNegative()) {
334+
} elseif ($b->isInfinite()) {
335+
if ($this->hasSameSign($b)) {
334336
return self::getPositiveInfinite();
335337
} else { // elseif ($this->isPositive() && $b->isNegative() || $this->isNegative() && $b->isPositive()) {
336338
return self::getNegativeInfinite();
@@ -655,6 +657,10 @@ public function abs()
655657
return $this->additiveInverse();
656658
}
657659

660+
public function hasSameSign(Decimal $b) {
661+
return $this->isPositive() && $b->isPositive() || $this->isNegative() && $b->isNegative();
662+
}
663+
658664
/**
659665
* "Rounds" the decimal string to have at most $scale digits after the point
660666
*

0 commit comments

Comments
 (0)