Skip to content

Commit 4ff57ba

Browse files
committed
Refactor bc_divide() to return bool
Return false on division by 0 attempt instead of -1 and true on success instead of 0
1 parent 09b89ba commit 4ff57ba

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

ext/bcmath/bcmath.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,13 @@ PHP_FUNCTION(bcdiv)
344344
goto cleanup;
345345
}
346346

347-
switch (bc_divide(first, second, &result, scale)) {
348-
case 0: /* OK */
349-
RETVAL_STR(bc_num2str_ex(result, scale));
350-
break;
351-
case -1: /* division by zero */
352-
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
353-
break;
347+
if (!bc_divide(first, second, &result, scale)) {
348+
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
349+
goto cleanup;
354350
}
355351

352+
RETVAL_STR(bc_num2str_ex(result, scale));
353+
356354
cleanup: {
357355
bc_free_num(&first);
358356
bc_free_num(&second);

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void bc_sub(bc_num n1, bc_num n2, bc_num *result, size_t scale_min);
118118

119119
void bc_multiply(bc_num n1, bc_num n2, bc_num *prod, size_t scale);
120120

121-
int bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale);
121+
bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale);
122122

123123
bool bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale);
124124

ext/bcmath/libbcmath/src/div.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ static void _one_mult(unsigned char *num, size_t size, int digit, unsigned char
7272

7373

7474
/* The full division routine. This computes N1 / N2. It returns
75-
0 if the division is ok and the result is in QUOT. The number of
76-
digits after the decimal point is SCALE. It returns -1 if division
75+
true if the division is ok and the result is in QUOT. The number of
76+
digits after the decimal point is SCALE. It returns false if division
7777
by zero is tried. The algorithm is found in Knuth Vol 2. p237. */
78-
int bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
78+
bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
7979
{
8080
bc_num qval;
8181
unsigned char *num1, *num2;
@@ -89,7 +89,7 @@ int bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
8989

9090
/* Test for divide by zero. */
9191
if (bc_is_zero(n2)) {
92-
return -1;
92+
return false;
9393
}
9494

9595
/* Test for divide by 1. If it is we must truncate. */
@@ -254,5 +254,5 @@ int bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
254254
efree(num2);
255255

256256
/* Everything is OK. */
257-
return 0;
257+
return true;
258258
}

0 commit comments

Comments
 (0)