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

Commit 96fbf6e

Browse files
committed
Many fixes, new failing tests
1 parent 944a883 commit 96fbf6e

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

src/DecimalConstants.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
*/
1313
final class DecimalConstants
1414
{
15+
private static $ZERO = null;
16+
private static $ONE = null;
17+
1518
private static $PI = null;
1619
private static $E = null;
1720
private static $EulerMascheroni = null;
@@ -36,6 +39,22 @@ private function __clone()
3639

3740
}
3841

42+
public static function Zero()
43+
{
44+
if (self::$ZERO === null) {
45+
self::$ZERO = Decimal::fromString("0");
46+
}
47+
return self::$ZERO;
48+
}
49+
50+
public static function One()
51+
{
52+
if (self::$ONE === null) {
53+
self::$ONE = Decimal::fromString("1");
54+
}
55+
return self::$ONE;
56+
}
57+
3958
/**
4059
* Returns the Pi number.
4160
* @return Decimal

src/InfiniteDecimal.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Litipk\BigNumbers;
44

55
use Litipk\BigNumbers\Decimal as Decimal;
6+
use Litipk\BigNumbers\DecimalConstants as DecimalConstants;
67
use Litipk\Exceptions\InvalidCastException;
8+
use Litipk\Exceptions\NotImplementedException;
79

810
/**
911
* Immutable object that represents an infinite number
@@ -26,7 +28,6 @@ class InfiniteDecimal extends Decimal
2628

2729
/**
2830
* Private constructor
29-
* @param integer $scale
3031
* @param string $value
3132
*/
3233
private function __construct($value)
@@ -168,6 +169,34 @@ public function sqrt($scale = null)
168169
return $this;
169170
}
170171

172+
/**
173+
* Powers this value to $b
174+
*
175+
* @param Decimal $b exponent
176+
* @param integer $scale
177+
* @return Decimal
178+
*/
179+
public function pow(Decimal $b, $scale = null)
180+
{
181+
if ($b->isPositive()) {
182+
if ($this->isPositive()) {
183+
return $this;
184+
}
185+
186+
// if ($this->isNegative())
187+
if ($b->isInfinite()) {
188+
throw new \DomainException("Negative infinite elevated to infinite is undefined.");
189+
}
190+
191+
192+
193+
throw new NotImplementedException("See issues #21, #22, #23 and #24 on Github.");
194+
195+
} else if ($b->isNegative()) {
196+
return DecimalConstants::Zero();
197+
}
198+
}
199+
171200
/**
172201
* Returns the object's logarithm in base 10
173202
* @param integer $scale
@@ -199,6 +228,7 @@ public function equals(Decimal $b, $scale = null)
199228
* $this > $b : returns 1 , $this < $b : returns -1 , $this == $b : returns 0
200229
*
201230
* @param Decimal $b
231+
* @param integer $scale
202232
* @return integer
203233
*/
204234
public function comp(Decimal $b, $scale = null)
@@ -282,6 +312,7 @@ public function tan($scale = null)
282312
}
283313

284314
/**
315+
* @param integer $scale Has no effect, exists only for compatibility.
285316
* @return boolean
286317
*/
287318
public function isZero($scale = null)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
use Litipk\BigNumbers\InfiniteDecimal as InfiniteDecimal;
5+
use Litipk\BigNumbers\DecimalConstants as DecimalConstants;
6+
7+
8+
date_default_timezone_set('UTC');
9+
10+
11+
class InfiniteDecimalPowTest extends PHPUnit_Framework_TestCase
12+
{
13+
public function testPositiveInfinitePositivePower()
14+
{
15+
$pInf = InfiniteDecimal::getPositiveInfinite();
16+
17+
$this->assertTrue($pInf->pow(Decimal::fromInteger(1))->equals($pInf));
18+
$this->assertTrue($pInf->pow(Decimal::fromInteger(2))->equals($pInf));
19+
$this->assertTrue($pInf->pow(Decimal::fromInteger(3))->equals($pInf));
20+
21+
$this->assertTrue($pInf->pow($pInf)->equals($pInf));
22+
}
23+
24+
public function testPositiveInfiniteNegativePower()
25+
{
26+
$pInf = InfiniteDecimal::getPositiveInfinite();
27+
$nInf = InfiniteDecimal::getNegativeInfinite();
28+
$zero = DecimalConstants::Zero();
29+
30+
$this->assertTrue($pInf->pow(Decimal::fromInteger(-1))->equals($zero));
31+
$this->assertTrue($pInf->pow(Decimal::fromInteger(-2))->equals($zero));
32+
$this->assertTrue($pInf->pow(Decimal::fromInteger(-3))->equals($zero));
33+
34+
$this->assertTrue($pInf->pow($nInf)->equals($zero));
35+
}
36+
37+
public function testNegativeInfinitePositiveFinitePower()
38+
{
39+
$pInf = InfiniteDecimal::getPositiveInfinite();
40+
$nInf = InfiniteDecimal::getNegativeInfinite();
41+
42+
$this->assertTrue($nInf->pow(Decimal::fromInteger(1))->equals($nInf));
43+
$this->assertTrue($nInf->pow(Decimal::fromInteger(2))->equals($pInf));
44+
$this->assertTrue($nInf->pow(Decimal::fromInteger(3))->equals($nInf));
45+
$this->assertTrue($nInf->pow(Decimal::fromInteger(4))->equals($pInf));
46+
}
47+
48+
public function testNegativeInfinitePositiveInfinitePower()
49+
{
50+
$this->assertTrue(false);
51+
}
52+
53+
public function testNegativeInfiniteNegativePower()
54+
{
55+
$nInf = InfiniteDecimal::getNegativeInfinite();
56+
$zero = DecimalConstants::Zero();
57+
58+
$this->assertTrue($nInf->pow(Decimal::fromInteger(-1))->equals($zero));
59+
$this->assertTrue($nInf->pow(Decimal::fromInteger(-2))->equals($zero));
60+
$this->assertTrue($nInf->pow(Decimal::fromInteger(-3))->equals($zero));
61+
62+
$this->assertTrue($nInf->pow($nInf)->equals($zero));
63+
}
64+
65+
public function testPositiveInfiniteZeroPower()
66+
{
67+
$this->assertTrue(false);
68+
}
69+
70+
public function testNegativeInfiniteZeroPower()
71+
{
72+
$this->assertTrue(false);
73+
}
74+
}

0 commit comments

Comments
 (0)