Skip to content

Commit 62878da

Browse files
committed
Fixed the behavior of the bcpow() function to be the same as the Number class.
1 parent c048993 commit 62878da

File tree

4 files changed

+74
-31
lines changed

4 files changed

+74
-31
lines changed

ext/bcmath/bcmath.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,10 @@ PHP_FUNCTION(bcpow)
615615
goto cleanup;
616616
}
617617

618-
bc_raise(first, exponent, &result, scale);
618+
if (!bc_raise(first, exponent, &result, scale)) {
619+
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Negative power of zero");
620+
goto cleanup;
621+
}
619622

620623
RETVAL_NEW_STR(bc_num2str_ex(result, scale));
621624

ext/bcmath/tests/bcpow.phpt

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,35 @@ bcmath
66
bcmath.scale=0
77
--FILE--
88
<?php
9-
require(__DIR__ . "/run_bcmath_tests_function.inc");
9+
// slightly modified run_bcmath_tests_function.inc for bcpow testing
10+
const STRING_PADDING = 30;
11+
function run_bcmath_tests(
12+
$firstTerms,
13+
$secondTerms,
14+
$symbol,
15+
$bcmath_function
16+
) {
17+
$scales = [0, 10];
18+
foreach ($scales as $scale) {
19+
foreach ($firstTerms as $firstTerm) {
20+
echo "Number \"$firstTerm\" (scale $scale)\n";
21+
foreach ($secondTerms as $secondTerm) {
22+
if (in_array($firstTerm, ['0', '-0'], true) && $secondTerm[0] === '-') {
23+
$ret = 'skip negative power of zero';
24+
} else {
25+
$ret = $bcmath_function($firstTerm, $secondTerm, $scale);
26+
}
27+
echo $firstTerm,
28+
" $symbol ",
29+
str_pad($secondTerm, STRING_PADDING),
30+
" = ",
31+
$ret,
32+
"\n";
33+
}
34+
echo "\n";
35+
}
36+
}
37+
}
1038

1139
$exponents = ["15", "-15", "1", "-9", "0", "-0"];
1240
$baseNumbers = array_merge($exponents, [
@@ -60,19 +88,19 @@ Number "-9" (scale 0)
6088

6189
Number "0" (scale 0)
6290
0 ** 15 = 0
63-
0 ** -15 = 0
91+
0 ** -15 = skip negative power of zero
6492
0 ** 1 = 0
65-
0 ** -9 = 0
93+
0 ** -9 = skip negative power of zero
6694
0 ** 0 = 1
67-
0 ** -0 = 1
95+
0 ** -0 = skip negative power of zero
6896

6997
Number "-0" (scale 0)
7098
-0 ** 15 = 0
71-
-0 ** -15 = 0
99+
-0 ** -15 = skip negative power of zero
72100
-0 ** 1 = 0
73-
-0 ** -9 = 0
101+
-0 ** -9 = skip negative power of zero
74102
-0 ** 0 = 1
75-
-0 ** -0 = 1
103+
-0 ** -0 = skip negative power of zero
76104

77105
Number "14.14" (scale 0)
78106
14.14 ** 15 = 180609729388653367
@@ -188,19 +216,19 @@ Number "-9" (scale 10)
188216

189217
Number "0" (scale 10)
190218
0 ** 15 = 0.0000000000
191-
0 ** -15 = 0.0000000000
219+
0 ** -15 = skip negative power of zero
192220
0 ** 1 = 0.0000000000
193-
0 ** -9 = 0.0000000000
221+
0 ** -9 = skip negative power of zero
194222
0 ** 0 = 1.0000000000
195-
0 ** -0 = 1.0000000000
223+
0 ** -0 = skip negative power of zero
196224

197225
Number "-0" (scale 10)
198226
-0 ** 15 = 0.0000000000
199-
-0 ** -15 = 0.0000000000
227+
-0 ** -15 = skip negative power of zero
200228
-0 ** 1 = 0.0000000000
201-
-0 ** -9 = 0.0000000000
229+
-0 ** -9 = skip negative power of zero
202230
-0 ** 0 = 1.0000000000
203-
-0 ** -0 = 1.0000000000
231+
-0 ** -0 = skip negative power of zero
204232

205233
Number "14.14" (scale 10)
206234
14.14 ** 15 = 180609729388653367.2586094856
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
bcpow() negative power of zero
3+
--EXTENSIONS--
4+
bcmath
5+
--INI--
6+
bcmath.scale=0
7+
--FILE--
8+
<?php
9+
$exponents = ["-15", "-1", "-9"];
10+
$baseNumbers = ['0', '-0'];
11+
12+
foreach ($baseNumbers as $baseNumber) {
13+
foreach ($exponents as $exponent) {
14+
try {
15+
echo bcpow($baseNumber, $exponent), "\n";
16+
} catch (Error $e) {
17+
echo $e->getMessage(), "\n";
18+
}
19+
}
20+
}
21+
22+
?>
23+
--EXPECT--
24+
Negative power of zero
25+
Negative power of zero
26+
Negative power of zero
27+
Negative power of zero
28+
Negative power of zero
29+
Negative power of zero

ext/bcmath/tests/gh16236.phpt

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)