Skip to content

Commit d66854b

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Fix #78238: BCMath returns "-0"
2 parents a733b1a + 9fbcaa5 commit d66854b

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ int bc_compare(bc_num n1, bc_num n2);
110110

111111
char bc_is_zero(bc_num num);
112112

113+
char bc_is_zero_for_scale(bc_num num, int scale);
114+
113115
char bc_is_near_zero(bc_num num, int scale);
114116

115117
char bc_is_neg(bc_num num);

ext/bcmath/libbcmath/src/num2str.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ zend_string
5050
int index, signch;
5151

5252
/* Allocate the string memory. */
53-
signch = num->n_sign != PLUS; /* Number of sign chars. */
53+
signch = num->n_sign != PLUS && !bc_is_zero_for_scale(num, MIN(num->n_scale, scale)); /* Number of sign chars. */
5454
if (scale > 0)
5555
str = zend_string_alloc(num->n_len + scale + signch + 1, 0);
5656
else

ext/bcmath/libbcmath/src/zero.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
/* In some places we need to check if the number NUM is zero. */
4141

4242
char
43-
bc_is_zero (bc_num num)
43+
bc_is_zero_for_scale (bc_num num, int scale)
4444
{
4545
int count;
4646
char *nptr;
@@ -49,7 +49,7 @@ bc_is_zero (bc_num num)
4949
if (num == BCG(_zero_)) return TRUE;
5050

5151
/* Initialize */
52-
count = num->n_len + num->n_scale;
52+
count = num->n_len + scale;
5353
nptr = num->n_value;
5454

5555
/* The check */
@@ -60,3 +60,9 @@ bc_is_zero (bc_num num)
6060
else
6161
return TRUE;
6262
}
63+
64+
char
65+
bc_is_zero (bc_num num)
66+
{
67+
return bc_is_zero_for_scale(num, num->n_scale);
68+
}

ext/bcmath/tests/bug78238.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug #78238 (BCMath returns "-0")
3+
--EXTENSIONS--
4+
bcmath
5+
--FILE--
6+
<?php
7+
var_dump(bcadd("0", "-0.1"));
8+
9+
$a = bcmul("0.9", "-0.1", 1);
10+
$b = bcmul("0.90", "-0.1", 1);
11+
var_dump($a, $b);
12+
?>
13+
--EXPECT--
14+
string(1) "0"
15+
string(3) "0.0"
16+
string(3) "0.0"

0 commit comments

Comments
 (0)