1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Values; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Samsara\Fermat\Types\Decimal; |
7
|
|
|
|
8
|
|
|
class LogTests extends TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/* |
12
|
|
|
* LN() |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
public function logImmutableProvider(): array |
16
|
|
|
{ |
17
|
|
|
$a = new ImmutableDecimal(1); |
18
|
|
|
$b = new ImmutableDecimal(2); |
19
|
|
|
$c = new ImmutableDecimal(3); |
20
|
|
|
$d = new ImmutableDecimal(10); |
21
|
|
|
$e = new ImmutableDecimal(100000000000); |
22
|
|
|
$f = new ImmutableDecimal('1000000000000000000000000000000'); |
23
|
|
|
$g = new ImmutableDecimal('0.0000000001'); |
24
|
|
|
|
25
|
|
|
return [ |
26
|
|
|
'IDecimal ln(1)' => [$a, '0', 10], |
27
|
|
|
'IDecimal ln(2)' => [$b, '0.6931471806', 10], |
28
|
|
|
'IDecimal ln(3)' => [$c, '1.0986122887', 10], |
29
|
|
|
'IDecimal ln(10)' => [$d, '2.302585093', 10], |
30
|
|
|
'IDecimal ln(100000000000)' => [$e, '25.3284360229', 10], |
31
|
|
|
'IDecimal ln(1000000000000000000000000000000)' => [$f, '69.0775527898', 10], |
32
|
|
|
'IDecimal ln(0.0000000001)' => [$g, '-23.0258509299', 10], |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function logMutableProvider(): array |
37
|
|
|
{ |
38
|
|
|
$a = new MutableDecimal(1); |
39
|
|
|
$b = new MutableDecimal(2); |
40
|
|
|
$c = new MutableDecimal(3); |
41
|
|
|
$d = new MutableDecimal(10); |
42
|
|
|
$e = new MutableDecimal(100000000000); |
43
|
|
|
$f = new MutableDecimal('1000000000000000000000000000000'); |
44
|
|
|
$g = new MutableDecimal('0.0000000001'); |
45
|
|
|
|
46
|
|
|
return [ |
47
|
|
|
'MDecimal ln(1)' => [$a, '0', 10], |
48
|
|
|
'MDecimal ln(2)' => [$b, '0.6931471806', 10], |
49
|
|
|
'MDecimal ln(3)' => [$c, '1.0986122887', 10], |
50
|
|
|
'MDecimal ln(10)' => [$d, '2.302585093', 10], |
51
|
|
|
'MDecimal ln(100000000000)' => [$e, '25.3284360229', 10], |
52
|
|
|
'MDecimal ln(1000000000000000000000000000000)' => [$f, '69.0775527898', 10], |
53
|
|
|
'MDecimal ln(0.0000000001)' => [$g, '-23.0258509299', 10], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @dataProvider logImmutableProvider |
59
|
|
|
* @dataProvider logMutableProvider |
60
|
|
|
*/ |
61
|
|
|
public function testLn(Decimal $a, string $expected, int $scale) |
62
|
|
|
{ |
63
|
|
|
if (str_contains($expected, 'Exception')) { |
64
|
|
|
$this->expectException($expected); |
65
|
|
|
$a->ln(); |
66
|
|
|
} else { |
67
|
|
|
$answer = $a->ln(); |
68
|
|
|
$this->assertEquals($expected, $answer->getValue()); |
69
|
|
|
$this->assertEquals($scale, $answer->getScale()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/* |
74
|
|
|
* LOG10() |
75
|
|
|
*/ |
76
|
|
|
|
77
|
|
|
public function log10ImmutableProvider(): array |
78
|
|
|
{ |
79
|
|
|
$a = new ImmutableDecimal(1); |
80
|
|
|
$b = new ImmutableDecimal(2); |
81
|
|
|
$c = new ImmutableDecimal(3); |
82
|
|
|
$d = new ImmutableDecimal(10); |
83
|
|
|
$e = new ImmutableDecimal(100000000000); |
84
|
|
|
$f = new ImmutableDecimal('1000000000000000000000000000000'); |
85
|
|
|
$g = new ImmutableDecimal('0.0000000001'); |
86
|
|
|
|
87
|
|
|
return [ |
88
|
|
|
'IDecimal log10(1)' => [$a, '0', 10], |
89
|
|
|
'IDecimal log10(2)' => [$b, '0.3010299957', 10], |
90
|
|
|
'IDecimal log10(3)' => [$c, '0.4771212547', 10], |
91
|
|
|
'IDecimal log10(10)' => [$d, '1', 10], |
92
|
|
|
'IDecimal log10(100000000000)' => [$e, '11', 10], |
93
|
|
|
'IDecimal log10(1000000000000000000000000000000)' => [$f, '30', 10], |
94
|
|
|
'IDecimal log10(0.0000000001)' => [$g, '-10', 10], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function log10MutableProvider(): array |
99
|
|
|
{ |
100
|
|
|
$a = new MutableDecimal(1); |
101
|
|
|
$b = new MutableDecimal(2); |
102
|
|
|
$c = new MutableDecimal(3); |
103
|
|
|
$d = new MutableDecimal(10); |
104
|
|
|
$e = new MutableDecimal(100000000000); |
105
|
|
|
$f = new MutableDecimal('1000000000000000000000000000000'); |
106
|
|
|
$g = new MutableDecimal('0.0000000001'); |
107
|
|
|
|
108
|
|
|
return [ |
109
|
|
|
'MDecimal log10(1)' => [$a, '0', 10], |
110
|
|
|
'MDecimal log10(2)' => [$b, '0.3010299957', 10], |
111
|
|
|
'MDecimal log10(3)' => [$c, '0.4771212547', 10], |
112
|
|
|
'MDecimal log10(10)' => [$d, '1', 10], |
113
|
|
|
'MDecimal log10(100000000000)' => [$e, '11', 10], |
114
|
|
|
'MDecimal log10(1000000000000000000000000000000)' => [$f, '30', 10], |
115
|
|
|
'MDecimal log10(0.0000000001)' => [$g, '-10', 10], |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @dataProvider log10ImmutableProvider |
121
|
|
|
* @dataProvider log10MutableProvider |
122
|
|
|
*/ |
123
|
|
|
public function testLog10(Decimal $a, string $expected, int $scale) |
124
|
|
|
{ |
125
|
|
|
if (str_contains($expected, 'Exception')) { |
126
|
|
|
$this->expectException($expected); |
127
|
|
|
$a->log10(); |
128
|
|
|
} else { |
129
|
|
|
$answer = $a->log10(); |
130
|
|
|
$this->assertEquals($expected, $answer->getValue()); |
131
|
|
|
$this->assertEquals($scale, $answer->getScale()); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/* |
136
|
|
|
* EXP() |
137
|
|
|
*/ |
138
|
|
|
|
139
|
|
|
public function expImmutableProvider(): array |
140
|
|
|
{ |
141
|
|
|
$a = new ImmutableDecimal(1); |
142
|
|
|
$b = new ImmutableDecimal(2); |
143
|
|
|
$c = new ImmutableDecimal(3); |
144
|
|
|
$d = new ImmutableDecimal(10); |
145
|
|
|
$e = new ImmutableDecimal('0.0000000001'); |
146
|
|
|
|
147
|
|
|
return [ |
148
|
|
|
'IDecimal exp(1)' => [$a, '2.7182818285', 10], |
149
|
|
|
'IDecimal exp(2)' => [$b, '7.3890560989', 10], |
150
|
|
|
'IDecimal exp(3)' => [$c, '20.0855369232', 10], |
151
|
|
|
'IDecimal exp(10)' => [$d, '22026.4657948067', 10], |
152
|
|
|
'IDecimal exp(0.0000000001)' => [$e, '1.0000000001', 10], |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function expMutableProvider(): array |
157
|
|
|
{ |
158
|
|
|
$a = new MutableDecimal(1); |
159
|
|
|
$b = new MutableDecimal(2); |
160
|
|
|
$c = new MutableDecimal(3); |
161
|
|
|
$d = new MutableDecimal(10); |
162
|
|
|
$e = new MutableDecimal('0.0000000001'); |
163
|
|
|
|
164
|
|
|
return [ |
165
|
|
|
'MDecimal exp(1)' => [$a, '2.7182818285', 10], |
166
|
|
|
'MDecimal exp(2)' => [$b, '7.3890560989', 10], |
167
|
|
|
'MDecimal exp(3)' => [$c, '20.0855369232', 10], |
168
|
|
|
'MDecimal exp(10)' => [$d, '22026.4657948067', 10], |
169
|
|
|
'MDecimal exp(0.0000000001)' => [$e, '1.0000000001', 10], |
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @dataProvider expImmutableProvider |
175
|
|
|
* @dataProvider expMutableProvider |
176
|
|
|
*/ |
177
|
|
|
public function testExp(Decimal $a, string $expected, int $scale) |
178
|
|
|
{ |
179
|
|
|
if (str_contains($expected, 'Exception')) { |
180
|
|
|
$this->expectException($expected); |
181
|
|
|
$a->exp(); |
182
|
|
|
} else { |
183
|
|
|
$answer = $a->exp(); |
184
|
|
|
$this->assertEquals($expected, $answer->getValue()); |
185
|
|
|
$this->assertEquals($scale, $answer->getScale()); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |