1
1
<?php
2
2
3
3
use Litipk \BigNumbers \Decimal as Decimal ;
4
+ use Litipk \BigNumbers \DecimalConstants as DecimalConstants ;
4
5
use Litipk \Exceptions \NotImplementedException as NotImplementedException ;
5
6
6
7
@@ -17,9 +18,12 @@ public function testZeroPositive()
17
18
$ this ->assertTrue ($ zero ->pow ($ two )->isZero ());
18
19
}
19
20
21
+ /**
22
+ * TODO : Split tests, change idiom to take exception message into account.
23
+ */
20
24
public function testZeroNoPositive ()
21
25
{
22
- $ zero = Decimal:: fromInteger ( 0 );
26
+ $ zero = DecimalConstants:: Zero ( );
23
27
$ nTwo = Decimal::fromInteger (-2 );
24
28
25
29
$ catched = false ;
@@ -41,8 +45,8 @@ public function testZeroNoPositive()
41
45
42
46
public function testNoZeroZero ()
43
47
{
44
- $ zero = Decimal:: fromInteger ( 0 );
45
- $ one = Decimal:: fromInteger ( 1 );
48
+ $ zero = DecimalConstants:: Zero ( );
49
+ $ one = DecimalConstants:: One ( );
46
50
47
51
$ nTwo = Decimal::fromInteger (-2 );
48
52
$ pTwo = Decimal::fromInteger (2 );
@@ -87,6 +91,9 @@ public function testBigPositiveSquareRoot()
87
91
$ this ->assertTrue ($ bignum1 ->pow ($ half , 6 )->equals ($ bignum1 ->sqrt (6 )));
88
92
}
89
93
94
+ /**
95
+ * TODO : Incorrect test! (The exception type should be changed, and the "idiom"!)
96
+ */
90
97
public function testNegativeSquareRoot ()
91
98
{
92
99
$ half = Decimal::fromString ('0.5 ' );
@@ -100,4 +107,52 @@ public function testNegativeSquareRoot()
100
107
}
101
108
$ this ->assertTrue ($ catched );
102
109
}
110
+
111
+ public function testPositiveWithNegativeExponent ()
112
+ {
113
+ $ pFive = Decimal::fromInteger (5 );
114
+
115
+ $ this ->assertTrue (
116
+ $ pFive ->pow (Decimal::fromInteger (-1 ))->equals (Decimal::fromString ("0.2 " )),
117
+ "The answer must be 0.2, but was " . $ pFive ->pow (Decimal::fromInteger (-1 ))
118
+ );
119
+ $ this ->assertTrue (
120
+ $ pFive ->pow (Decimal::fromInteger (-2 ))->equals (Decimal::fromString ("0.04 " )),
121
+ "The answer must be 0.04, but was " . $ pFive ->pow (Decimal::fromInteger (-2 ))
122
+ );
123
+ $ this ->assertTrue (
124
+ $ pFive ->pow (Decimal::fromInteger (-3 ))->equals (Decimal::fromString ("0.008 " )),
125
+ "The answer must be 0.008, but was " . $ pFive ->pow (Decimal::fromInteger (-3 ))
126
+ );
127
+ $ this ->assertTrue (
128
+ $ pFive ->pow (Decimal::fromInteger (-4 ))->equals (Decimal::fromString ("0.0016 " )),
129
+ "The answer must be 0.0016, but was " . $ pFive ->pow (Decimal::fromInteger (-4 ))
130
+ );
131
+
132
+ $ this ->assertTrue (
133
+ $ pFive ->pow (Decimal::fromFloat (-4.5 ))->equals (Decimal::fromString ("0.00071554 " )),
134
+ "The answer must be 0.00071554, but was " . $ pFive ->pow (Decimal::fromFloat (-4.5 ))
135
+ );
136
+ }
137
+
138
+ public function testNegativeWithPositiveExponent ()
139
+ {
140
+ $ nFive = Decimal::fromInteger (-5 );
141
+
142
+ $ this ->assertTrue ($ nFive ->pow (DecimalConstants::One ())->equals ($ nFive ));
143
+ $ this ->assertTrue ($ nFive ->pow (Decimal::fromInteger (2 ))->equals (Decimal::fromInteger (25 )));
144
+ $ this ->assertTrue ($ nFive ->pow (Decimal::fromInteger (3 ))->equals (Decimal::fromInteger (-125 )));
145
+ }
146
+
147
+ public function testNegativeWithNegativeExponent ()
148
+ {
149
+ $ nFive = Decimal::fromInteger (-5 );
150
+
151
+ $ this ->assertTrue (
152
+ $ nFive ->pow (Decimal::fromInteger (-1 ))->equals (Decimal::fromString ("-0.2 " )),
153
+ "The answer must be -0.2, but was " . $ nFive ->pow (Decimal::fromInteger (-1 ))
154
+ );
155
+ $ this ->assertTrue ($ nFive ->pow (Decimal::fromInteger (-2 ))->equals (Decimal::fromString ("0.04 " )));
156
+ $ this ->assertTrue ($ nFive ->pow (Decimal::fromInteger (-3 ))->equals (Decimal::fromString ("-0.008 " )));
157
+ }
103
158
}
0 commit comments