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

Commit 1a30f42

Browse files
committed
Splitter again Decimal into Decimal & InfiniteDecimal
1 parent b9d7948 commit 1a30f42

File tree

4 files changed

+315
-98
lines changed

4 files changed

+315
-98
lines changed

src/Decimal.php

Lines changed: 21 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Litipk\BigNumbers;
44

5+
use Litipk\BigNumbers\InfiniteDecimal as InfiniteDecimal;
6+
57
use Litipk\Exceptions\NotImplementedException as NotImplementedException;
68
use Litipk\Exceptions\InvalidArgumentTypeException as InvalidArgumentTypeException;
79

@@ -10,25 +12,13 @@
1012
*
1113
* @author Andreu Correa Casablanca <[email protected]>
1214
*/
13-
final class Decimal
15+
class Decimal
1416
{
15-
/**
16-
* Single instance of "Positive Infinite"
17-
* @var Decimal
18-
*/
19-
private static $pInf = null;
20-
21-
/**
22-
* Single instance of "Negative Infinite"
23-
* @var Decimal
24-
*/
25-
private static $nInf = null;
26-
2717
/**
2818
* Internal numeric value
2919
* @var string
3020
*/
31-
private $value;
21+
protected $value;
3222

3323
/**
3424
* Number of digits behind the point
@@ -61,11 +51,7 @@ private function __clone()
6151
*/
6252
public static function getPositiveInfinite()
6353
{
64-
if (self::$pInf === null) {
65-
self::$pInf = new Decimal('INF', 0);
66-
}
67-
68-
return self::$pInf;
54+
return InfiniteDecimal::getPositiveInfinite();
6955
}
7056

7157
/**
@@ -74,11 +60,7 @@ public static function getPositiveInfinite()
7460
*/
7561
public static function getNegativeInfinite()
7662
{
77-
if (self::$nInf === null) {
78-
self::$nInf = new Decimal('-INF', 0);
79-
}
80-
81-
return self::$nInf;
63+
return InfiniteDecimal::getNegativeInfinite();
8264
}
8365

8466
/**
@@ -147,9 +129,9 @@ public static function fromFloat($fltValue, $scale = null)
147129
'$fltValue must be of type float'
148130
);
149131
} elseif ($fltValue === INF) {
150-
return Decimal::getPositiveInfinite();
132+
return InfiniteDecimal::getPositiveInfinite();
151133
} elseif ($fltValue === -INF) {
152-
return Decimal::getNegativeInfinite();
134+
return InfiniteDecimal::getNegativeInfinite();
153135
} elseif (is_nan($fltValue)) {
154136
throw new \DomainException(
155137
"To ensure consistency, this class doesn't handle NaN objects."
@@ -269,15 +251,7 @@ public function add(Decimal $b, $scale = null)
269251
{
270252
self::paramsValidation($b, $scale);
271253

272-
if ($this->isInfinite()) {
273-
if (!$b->isInfinite()) {
274-
return $this;
275-
} elseif ($this->hasSameSign($b)) {
276-
return $this;
277-
} else { // elseif ($this->isPositive() && $b->isNegative || $this->isNegative() && $b->isPositive()) {
278-
throw new \DomainException("Infinite numbers with opposite signs can't be added");
279-
}
280-
} elseif ($b->isInfinite()) {
254+
if ($b->isInfinite()) {
281255
return $b;
282256
}
283257

@@ -297,15 +271,7 @@ public function sub(Decimal $b, $scale = null)
297271
{
298272
self::paramsValidation($b, $scale);
299273

300-
if ($this->isInfinite()) {
301-
if (!$b->isInfinite()) {
302-
return $this;
303-
} elseif (!$this->hasSameSign($b)) {
304-
return $this;
305-
} else { // elseif () {
306-
throw new \DomainException("Infinite numbers with the same sign can't be subtracted");
307-
}
308-
} elseif ($b->isInfinite()) {
274+
if ($b->isInfinite()) {
309275
return $b->additiveInverse();
310276
}
311277

@@ -325,18 +291,10 @@ public function mul(Decimal $b, $scale = null)
325291
{
326292
self::paramsValidation($b, $scale);
327293

328-
if ($this->isInfinite() || $b->isZero()) {
294+
if ($b->isInfinite()) {
329295
return $b->mul($this);
330-
} elseif ($this->isZero() && $b->isInfinite()) {
331-
throw new \DomainException("Zero multiplied by infinite is not allowed.");
332-
} elseif ($this->isZero() && !$b->isInfinite()) {
296+
} elseif ($b->isZero()) {
333297
return Decimal::fromInteger(0, $scale);
334-
} elseif ($b->isInfinite()) {
335-
if ($this->hasSameSign($b)) {
336-
return self::getPositiveInfinite();
337-
} else { // elseif ($this->isPositive() && $b->isNegative() || $this->isNegative() && $b->isPositive()) {
338-
return self::getNegativeInfinite();
339-
}
340298
}
341299

342300
return self::fromString(
@@ -363,16 +321,6 @@ public function div(Decimal $b, $scale = null)
363321
throw new \DomainException("Division by zero is not allowed.");
364322
} elseif ($this->isZero()) {
365323
return self::fromDecimal($this, $scale);
366-
} elseif ($this->isInfinite()) {
367-
368-
if ($b->isInfinite()) {
369-
throw new \DomainException("Infinite divided by Infinite is not allowed.");
370-
} elseif ($b->isPositive()) {
371-
return $this;
372-
} else { //if ($b->isNegative()) {
373-
return $this->additiveInverse();
374-
}
375-
376324
} elseif ($b->isInfinite()) {
377325
return Decimal::fromInteger(0, $scale);
378326
} else {
@@ -417,8 +365,6 @@ public function sqrt($scale = null)
417365
);
418366
} elseif ($this->isZero()) {
419367
return Decimal::fromDecimal($this, $scale);
420-
} elseif ($this->isInfinite()) {
421-
return $this;
422368
}
423369

424370
$sqrt_scale = ($scale !== null ? $scale : $this->scale);
@@ -494,12 +440,10 @@ public function log10($scale = null)
494440
{
495441
if ($this->isNegative()) {
496442
throw new \DomainException(
497-
"Decimal can't handle logarithms of negative numbers (it's only for real numbers)"
443+
"Decimal can't handle logarithms of negative numbers (it's only for real numbers)."
498444
);
499445
} elseif ($this->isZero()) {
500-
return Decimal::getNegativeInfinite();
501-
} elseif ($this->isInfinite()) {
502-
return $this;
446+
return InfiniteDecimal::getNegativeInfinite();
503447
}
504448

505449
return self::fromString(
@@ -513,10 +457,6 @@ public function log10($scale = null)
513457
*/
514458
public function isZero($scale = null)
515459
{
516-
if ($this->isInfinite()) {
517-
return false;
518-
}
519-
520460
$cmp_scale = $scale !== null ? $scale : $this->scale;
521461

522462
return (bccomp(self::innerRound($this->value, $cmp_scale), '0', $cmp_scale) === 0);
@@ -543,7 +483,7 @@ public function isNegative()
543483
*/
544484
public function isInfinite()
545485
{
546-
return ($this === self::$pInf || $this === self::$nInf);
486+
return false;
547487
}
548488

549489
/**
@@ -558,7 +498,7 @@ public function equals(Decimal $b, $scale = null)
558498

559499
if ($this === $b) {
560500
return true;
561-
} elseif ($this->isInfinite()) {
501+
} elseif ($b->isInfinite()) {
562502
return false;
563503
} else {
564504
$cmp_scale = $scale !== null ? $scale : max($this->scale, $b->scale);
@@ -585,10 +525,8 @@ public function comp(Decimal $b, $scale = null)
585525

586526
if ($this === $b) {
587527
return 0;
588-
} elseif ($this === self::getPositiveInfinite() || $b === self::getNegativeInfinite()) {
589-
return 1;
590-
} elseif ($this === self::getNegativeInfinite() || $b === self::getPositiveInfinite()) {
591-
return -1;
528+
} elseif ($b->isInfinite()) {
529+
return -$b->comp($this);
592530
}
593531

594532
return bccomp(
@@ -606,13 +544,7 @@ public function additiveInverse()
606544
{
607545
if ($this->isZero()) {
608546
return $this;
609-
} elseif ($this === self::getPositiveInfinite()) {
610-
return self::$nInf;
611-
} elseif ($this === self::getNegativeInfinite()) {
612-
return self::$pInf;
613-
}
614-
615-
if ($this->isNegative()) {
547+
} elseif ($this->isNegative()) {
616548
$value = substr($this->value, 1);
617549
} else { // if ($this->isPositive()) {
618550
$value = '-' . $this->value;
@@ -628,7 +560,7 @@ public function additiveInverse()
628560
*/
629561
public function round($scale = 0)
630562
{
631-
if ($scale >= $this->scale || $this->isInfinite()) {
563+
if ($scale >= $this->scale) {
632564
return $this;
633565
}
634566

@@ -798,7 +730,7 @@ private static function compute2NRoot($base, $index, $out_scale)
798730
* @param mixed $value
799731
* @param integer $scale
800732
*/
801-
private static function paramsValidation($value, $scale)
733+
protected static function paramsValidation($value, $scale)
802734
{
803735
if ($value === null) {
804736
throw new \InvalidArgumentException('$value must be a non null number');

0 commit comments

Comments
 (0)