Skip to content

Commit 674ec02

Browse files
Fixed the sign to be PLUS if the result is 0 (#15599)
1 parent 9b73d59 commit 674ec02

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

ext/bcmath/libbcmath/src/div.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,12 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
478478

479479
/* do divide */
480480
bc_do_div(numeratorend, numerator_readable_len, numerator_bottom_extension, divisorend, divisor_len, quot, quot_full_len);
481-
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
482481
_bc_rm_leading_zeros(*quot);
482+
if (bc_is_zero(*quot)) {
483+
(*quot)->n_sign = PLUS;
484+
} else {
485+
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
486+
}
483487

484488
return true;
485489
}

ext/bcmath/libbcmath/src/floor_or_ceil.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ bc_num bc_floor_or_ceil(bc_num num, bool is_floor)
3030
/* If the number is positive and we are flooring, then nothing else needs to be done.
3131
* Similarly, if the number is negative and we are ceiling, then nothing else needs to be done. */
3232
if (num->n_scale == 0 || result->n_sign == (is_floor ? PLUS : MINUS)) {
33-
return result;
33+
goto check_zero;
3434
}
3535

3636
/* check fractional part. */
@@ -43,12 +43,19 @@ bc_num bc_floor_or_ceil(bc_num num, bool is_floor)
4343

4444
/* If all digits past the decimal point are 0 */
4545
if (count == 0) {
46-
return result;
46+
goto check_zero;
4747
}
4848

4949
/* Increment the absolute value of the result by 1 and add sign information */
5050
bc_num tmp = _bc_do_add(result, BCG(_one_));
5151
tmp->n_sign = result->n_sign;
5252
bc_free_num(&result);
53-
return tmp;
53+
result = tmp;
54+
55+
check_zero:
56+
if (bc_is_zero(result)) {
57+
result->n_sign = PLUS;
58+
}
59+
60+
return result;
5461
}

ext/bcmath/libbcmath/src/round.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
7676
if (*nptr >= 5) {
7777
goto up;
7878
} else if (*nptr < 5) {
79-
return;
79+
goto check_zero;
8080
}
8181
break;
8282

@@ -86,14 +86,14 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
8686
if (*nptr > 5) {
8787
goto up;
8888
} else if (*nptr < 5) {
89-
return;
89+
goto check_zero;
9090
}
9191
/* if *nptr == 5, we need to look-up further digits before making a decision. */
9292
break;
9393

9494
case PHP_ROUND_CEILING:
9595
if (num->n_sign != PLUS) {
96-
return;
96+
goto check_zero;
9797
} else if (*nptr > 0) {
9898
goto up;
9999
}
@@ -102,15 +102,15 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
102102

103103
case PHP_ROUND_FLOOR:
104104
if (num->n_sign != MINUS) {
105-
return;
105+
goto check_zero;
106106
} else if (*nptr > 0) {
107107
goto up;
108108
}
109109
/* if *nptr == 0, a loop is required for judgment. */
110110
break;
111111

112112
case PHP_ROUND_TOWARD_ZERO:
113-
return;
113+
goto check_zero;
114114

115115
case PHP_ROUND_AWAY_FROM_ZERO:
116116
if (*nptr > 0) {
@@ -139,17 +139,17 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
139139
case PHP_ROUND_CEILING:
140140
case PHP_ROUND_FLOOR:
141141
case PHP_ROUND_AWAY_FROM_ZERO:
142-
return;
142+
goto check_zero;
143143

144144
case PHP_ROUND_HALF_EVEN:
145145
if (rounded_len == 0 || num->n_value[rounded_len - 1] % 2 == 0) {
146-
return;
146+
goto check_zero;
147147
}
148148
break;
149149

150150
case PHP_ROUND_HALF_ODD:
151151
if (rounded_len != 0 && num->n_value[rounded_len - 1] % 2 == 1) {
152-
return;
152+
goto check_zero;
153153
}
154154
break;
155155

@@ -176,4 +176,9 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
176176
bc_free_num(result);
177177
*result = tmp;
178178
}
179+
180+
check_zero:
181+
if (bc_is_zero(*result)) {
182+
(*result)->n_sign = PLUS;
183+
}
179184
}

0 commit comments

Comments
 (0)