Skip to content

Commit e8fe7e4

Browse files
ext/bcmath: Fixed bcdiv() div by one (#15629)
Fixed the incorrect scale that should be used when dividing by 1, that is, comparing the divisor and 1 to confirm equality. Additionally, have increased the number of test cases for bcdiv_by_pow_10.phpt.
1 parent 09d5b71 commit e8fe7e4

File tree

3 files changed

+404
-81
lines changed

3 files changed

+404
-81
lines changed

ext/bcmath/libbcmath/src/div.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
344344
}
345345

346346
/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
347-
if (_bc_do_compare(divisor, BCG(_one_), scale, false) == BCMATH_EQUAL) {
347+
if (_bc_do_compare(divisor, BCG(_one_), divisor->n_scale, false) == BCMATH_EQUAL) {
348348
size_t quot_scale = MIN(numerator->n_scale, scale);
349349
*quot = bc_new_num_nonzeroed(numerator->n_len, quot_scale);
350350
char *qptr = (*quot)->n_value;

ext/bcmath/tests/bcdiv_by_one.phpt

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
--TEST--
2+
bcdiv() function div by one
3+
--EXTENSIONS--
4+
bcmath
5+
--INI--
6+
bcmath.scale=0
7+
--FILE--
8+
<?php
9+
$dividend_cases = ['100', '-100'];
10+
$divisor_cases = ['1.010', '1.01', '1.1', '1', '-1.010', '-1.01', '-1.1', '-1'];
11+
$scale_cases = [0, 1, 2, 3];
12+
13+
foreach ($scale_cases as $scale) {
14+
echo "scale: {$scale}\n";
15+
foreach ($divisor_cases as $divisor) {
16+
foreach ($dividend_cases as $dividend) {
17+
$dividend_label = str_pad($dividend, 4, ' ', STR_PAD_LEFT);
18+
$divisor_label = str_pad($divisor, 6, ' ', STR_PAD_LEFT);
19+
$quot = bcdiv($dividend, $divisor, $scale);
20+
$quot_label = str_pad($quot, $scale + 2 + ($scale ? 1 : 0), ' ', STR_PAD_LEFT);
21+
echo $dividend_label, ' / ', $divisor_label, ' = ', $quot_label, "\n";
22+
}
23+
}
24+
echo "\n";
25+
}
26+
?>
27+
--EXPECT--
28+
scale: 0
29+
100 / 1.010 = 99
30+
-100 / 1.010 = -99
31+
100 / 1.01 = 99
32+
-100 / 1.01 = -99
33+
100 / 1.1 = 90
34+
-100 / 1.1 = -90
35+
100 / 1 = 100
36+
-100 / 1 = -100
37+
100 / -1.010 = -99
38+
-100 / -1.010 = 99
39+
100 / -1.01 = -99
40+
-100 / -1.01 = 99
41+
100 / -1.1 = -90
42+
-100 / -1.1 = 90
43+
100 / -1 = -100
44+
-100 / -1 = 100
45+
46+
scale: 1
47+
100 / 1.010 = 99.0
48+
-100 / 1.010 = -99.0
49+
100 / 1.01 = 99.0
50+
-100 / 1.01 = -99.0
51+
100 / 1.1 = 90.9
52+
-100 / 1.1 = -90.9
53+
100 / 1 = 100.0
54+
-100 / 1 = -100.0
55+
100 / -1.010 = -99.0
56+
-100 / -1.010 = 99.0
57+
100 / -1.01 = -99.0
58+
-100 / -1.01 = 99.0
59+
100 / -1.1 = -90.9
60+
-100 / -1.1 = 90.9
61+
100 / -1 = -100.0
62+
-100 / -1 = 100.0
63+
64+
scale: 2
65+
100 / 1.010 = 99.00
66+
-100 / 1.010 = -99.00
67+
100 / 1.01 = 99.00
68+
-100 / 1.01 = -99.00
69+
100 / 1.1 = 90.90
70+
-100 / 1.1 = -90.90
71+
100 / 1 = 100.00
72+
-100 / 1 = -100.00
73+
100 / -1.010 = -99.00
74+
-100 / -1.010 = 99.00
75+
100 / -1.01 = -99.00
76+
-100 / -1.01 = 99.00
77+
100 / -1.1 = -90.90
78+
-100 / -1.1 = 90.90
79+
100 / -1 = -100.00
80+
-100 / -1 = 100.00
81+
82+
scale: 3
83+
100 / 1.010 = 99.009
84+
-100 / 1.010 = -99.009
85+
100 / 1.01 = 99.009
86+
-100 / 1.01 = -99.009
87+
100 / 1.1 = 90.909
88+
-100 / 1.1 = -90.909
89+
100 / 1 = 100.000
90+
-100 / 1 = -100.000
91+
100 / -1.010 = -99.009
92+
-100 / -1.010 = 99.009
93+
100 / -1.01 = -99.009
94+
-100 / -1.01 = 99.009
95+
100 / -1.1 = -90.909
96+
-100 / -1.1 = 90.909
97+
100 / -1 = -100.000
98+
-100 / -1 = 100.000

0 commit comments

Comments
 (0)