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

Commit bad0151

Browse files
committed
Added new tests, many are failing
1 parent 3d1d245 commit bad0151

File tree

7 files changed

+83
-47
lines changed

7 files changed

+83
-47
lines changed

src/Decimal.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -166,35 +166,35 @@ public static function fromString($strValue, $scale = null)
166166
} elseif (preg_match('/^([+\-]?)0*(([1-9][0-9]*|[0-9])(\.[0-9]+)?)$/', $strValue, $captures) === 1) {
167167

168168
// Now it's time to strip leading zeros in order to normalize inner values
169-
$value = self::normalizeSign($captures[1]) . $captures[2];
170-
171-
$min_scale = isset($captures[4]) ?
172-
max(0, strlen($captures[4])-1) :
173-
0;
169+
$value = self::normalizeSign($captures[1]) . $captures[2];
170+
$min_scale = isset($captures[4]) ? max(0, strlen($captures[4]) - 1) : 0;
174171

175172
} elseif (preg_match('/([+\-]?)0*([0-9](\.[0-9]+)?)[eE]([+\-]?)(\d+)/', $strValue, $captures) === 1) {
176173

177-
$mantissa_scale = max(strlen($captures[3])-1, 0);
174+
$mantissa_scale = max(strlen($captures[3]) - 1, 0);
178175

179-
$exp_val = (int)$captures[5];
176+
$exp_val = (int)$captures[5];
180177

181178
if (self::normalizeSign($captures[4]) === '') {
182-
$min_scale = max($mantissa_scale-$exp_val, 0);
179+
$min_scale = max($mantissa_scale - $exp_val, 0);
183180
$tmp_multiplier = bcpow(10, $exp_val);
184181
} else {
185-
$min_scale = $mantissa_scale + $exp_val;
182+
$min_scale = $mantissa_scale + $exp_val;
186183
$tmp_multiplier = bcpow(10, -$exp_val, $exp_val);
187184
}
188185

189186
$value = self::normalizeSign($captures[1]) . bcmul(
190-
$captures[2],
191-
$tmp_multiplier,
192-
max(
193-
$min_scale,
194-
$scale !== null ? $scale : 0
195-
)
196-
);
187+
$captures[2],
188+
$tmp_multiplier,
189+
max($min_scale, $scale !== null ? $scale : 0)
190+
);
197191

192+
} else if (preg_match('/([+\-]?)(inf|Inf|INF)/', $strValue, $captures) === 1) {
193+
if ($captures[1] === '-') {
194+
return InfiniteDecimal::getNegativeInfinite();
195+
} else {
196+
return InfiniteDecimal::getPositiveInfinite();
197+
}
198198
} else {
199199
throw new \InvalidArgumentException(
200200
'$strValue must be a string that represents uniquely a float point number.'
@@ -207,10 +207,7 @@ public static function fromString($strValue, $scale = null)
207207
$dec_scale = $min_scale;
208208
}
209209

210-
return new Decimal(
211-
self::innerRound($value, $dec_scale),
212-
$dec_scale
213-
);
210+
return new Decimal(self::innerRound($value, $dec_scale), $dec_scale);
214211
}
215212

216213
/**
@@ -642,7 +639,7 @@ public function mod(Decimal $d, $scale = null)
642639
*/
643640
public function sin($scale = null) {
644641
// First normalise the number in the [0, 2PI] domain
645-
$twoPi = NumConstants::PI()->mul(Decimal::fromString("2"));
642+
$twoPi = DecimalConstants::PI()->mul(Decimal::fromString("2"));
646643
$x = $this->mod($twoPi);
647644
$zero = Decimal::fromString("0");
648645

@@ -694,7 +691,7 @@ public function sin($scale = null) {
694691
*/
695692
public function cos($scale = null) {
696693
// First normalise the number in the [0, 2PI] domain
697-
$twoPi = NumConstants::PI()->mul(Decimal::fromString("2"));
694+
$twoPi = DecimalConstants::PI()->mul(Decimal::fromString("2"));
698695
$x = $this->mod($twoPi);
699696
$zero = Decimal::fromString("0");
700697

src/NumConstants.php renamed to src/DecimalConstants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @author Andreu Correa Casablanca <[email protected]>
1212
*/
13-
class NumConstants
13+
class DecimalConstants
1414
{
1515
private static $PI = null;
1616
private static $E = null;

tests/Decimal/DecimalFromStringTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,33 @@ public function testExponentialNotation_With_ZeroExponent()
103103
);
104104
}
105105

106+
public function testStringInfinite()
107+
{
108+
$infUU = Decimal::fromString("INF");
109+
$infLL = Decimal::fromString("inf");
110+
$infUL = Decimal::fromString("Inf");
111+
112+
$pInfUU = Decimal::fromString("+INF");
113+
$pInfLL = Decimal::fromString("+inf");
114+
$pInfUL = Decimal::fromString("+Inf");
115+
116+
$nInfUU = Decimal::fromString("-INF");
117+
$nInfLL = Decimal::fromString("-inf");
118+
$nInfUL = Decimal::fromString("-Inf");
119+
120+
$this->assertTrue($infUU->equals(Decimal::getPositiveInfinite()));
121+
$this->assertTrue($infLL->equals(Decimal::getPositiveInfinite()));
122+
$this->assertTrue($infUL->equals(Decimal::getPositiveInfinite()));
123+
124+
$this->assertTrue($pInfUU->equals(Decimal::getPositiveInfinite()));
125+
$this->assertTrue($pInfLL->equals(Decimal::getPositiveInfinite()));
126+
$this->assertTrue($pInfUL->equals(Decimal::getPositiveInfinite()));
127+
128+
$this->assertTrue($nInfUU->equals(Decimal::getNegativeInfinite()));
129+
$this->assertTrue($nInfLL->equals(Decimal::getNegativeInfinite()));
130+
$this->assertTrue($nInfUL->equals(Decimal::getNegativeInfinite()));
131+
}
132+
106133
/**
107134
* @expectedException Litipk\Exceptions\InvalidArgumentTypeException
108135
* @expectedExceptionMessage $strVlue must be of type string.

tests/Decimal/DecimalLog10Test.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ public function testNegativeLog10()
2828
Decimal::fromInteger(-1)->log10();
2929
}
3030

31-
/**
32-
* @expectedException \DomainException
33-
* @expectedExceptionMessage Decimal can't handle logarithms of negative numbers (it's only for real numbers).
34-
*/
35-
public function testNegativeInfiniteLog10()
36-
{
37-
Decimal::getNegativeInfinite()->log10();
38-
}
39-
4031
public function testBigNumbersLog10()
4132
{
4233
$bignumber = Decimal::fromString(bcpow('10', '2417'));
@@ -61,11 +52,4 @@ public function testMediumNumbersLog10()
6152
$this->assertTrue($seventyfive->log10(5)->equals(Decimal::fromString('1.87506')));
6253
$this->assertTrue($fortynine->log10(7)->equals(Decimal::fromString('1.6901961')));
6354
}
64-
65-
public function testPInfiniteLog10()
66-
{
67-
$pInf = Decimal::getPositiveInfinite();
68-
69-
$this->assertTrue($pInf->log10()->equals($pInf));
70-
}
7155
}

tests/Decimal/DecimalModTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public function modProvider() {
1313
array('34', '3.4', '0'),
1414
array('15.1615', '3.156156', '2.536876'),
1515
array('15.1615', '3.156156', '2.5365', 3),
16+
array('3.4', '-2', '-0.6'),
17+
array('-3.4', '2', '0.6')
1618
);
1719
}
1820
/**
@@ -25,7 +27,7 @@ public function testMod($number, $mod, $answer, $scale = null) {
2527

2628
$this->assertTrue(
2729
Decimal::fromString($answer)->equals($decimalAnswer),
28-
$decimalAnswer . ' must be equal to ' . $answer
30+
$decimalAnswer . ' % ' . $mod . ' must be equal to ' . $answer . ', but was ' . $decimalAnswer
2931
);
3032
}
3133
}

tests/NumConstantsTest.php renamed to tests/DecimalConstantsTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
<?php
22

3-
use Litipk\BigNumbers\NumConstants as NumConstants;
3+
use Litipk\BigNumbers\DecimalConstants as DecimalConstants;
44
use Litipk\BigNumbers\Decimal as Decimal;
55

66

77
date_default_timezone_set('UTC');
88

99

10-
class NumConstantsTest extends PHPUnit_Framework_TestCase
10+
class DecimalConstantsTest extends PHPUnit_Framework_TestCase
1111
{
1212
public function testFiniteAbs()
1313
{
14-
$this->assertTrue(NumConstants::PI()->equals(
14+
$this->assertTrue(DecimalConstants::PI()->equals(
1515
Decimal::fromString("3.14159265358979323846264338327950")
1616
));
1717

18-
$this->assertTrue(NumConstants::E()->equals(
18+
$this->assertTrue(DecimalConstants::E()->equals(
1919
Decimal::fromString("2.71828182845904523536028747135266")
2020
));
2121

22-
$this->assertTrue(NumConstants::EulerMascheroni()->equals(
22+
$this->assertTrue(DecimalConstants::EulerMascheroni()->equals(
2323
Decimal::fromString("0.57721566490153286060651209008240")
2424
));
2525

26-
$this->assertTrue(NumConstants::GoldenRatio()->equals(
26+
$this->assertTrue(DecimalConstants::GoldenRatio()->equals(
2727
Decimal::fromString("1.61803398874989484820458683436564")
2828
));
2929

30-
$this->assertTrue(NumConstants::LightSpeed()->equals(
30+
$this->assertTrue(DecimalConstants::LightSpeed()->equals(
3131
Decimal::fromInteger(299792458)
3232
));
3333
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\InfiniteDecimal as InfiniteDecimal;
4+
5+
6+
date_default_timezone_set('UTC');
7+
8+
9+
class InfiniteDecimalLog10Test extends PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @expectedException \DomainException
13+
* @expectedExceptionMessage Decimal can't handle logarithms of negative numbers (it's only for real numbers).
14+
*/
15+
public function testNegativeInfiniteLog10()
16+
{
17+
InfiniteDecimal::getNegativeInfinite()->log10();
18+
}
19+
20+
public function testPInfiniteLog10()
21+
{
22+
$pInf = InfiniteDecimal::getPositiveInfinite();
23+
24+
$this->assertTrue($pInf->log10()->equals($pInf));
25+
}
26+
}

0 commit comments

Comments
 (0)