Skip to content

Commit 09b89ba

Browse files
committed
Refactor bc_divmod() and bc_modulo() to return bool
Return false on division by 0 attempt instead of -1 and true on success instead of 0
1 parent 80d172d commit 09b89ba

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

ext/bcmath/bcmath.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,13 @@ PHP_FUNCTION(bcmod)
400400
goto cleanup;
401401
}
402402

403-
switch (bc_modulo(first, second, &result, scale)) {
404-
case 0:
405-
RETVAL_STR(bc_num2str_ex(result, scale));
406-
break;
407-
case -1:
408-
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
409-
break;
403+
if (!bc_modulo(first, second, &result, scale)) {
404+
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
405+
goto cleanup;
410406
}
411407

408+
RETVAL_STR(bc_num2str_ex(result, scale));
409+
412410
cleanup: {
413411
bc_free_num(&first);
414412
bc_free_num(&second);

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ void bc_multiply(bc_num n1, bc_num n2, bc_num *prod, size_t scale);
120120

121121
int bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale);
122122

123-
int bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale);
123+
bool bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale);
124124

125-
int bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale);
125+
bool bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale);
126126

127127
typedef enum {
128128
OK,

ext/bcmath/libbcmath/src/divmod.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,26 @@
3030
*************************************************************************/
3131

3232
#include "bcmath.h"
33+
#include <stdbool.h>
3334
#include <stddef.h>
3435

3536

3637
/* Division *and* modulo for numbers. This computes both NUM1 / NUM2 and
3738
NUM1 % NUM2 and puts the results in QUOT and REM, except that if QUOT
3839
is NULL then that store will be omitted.
40+
false is returned if divisor is 0.
41+
true otherwise for success.
3942
*/
4043

41-
int bc_divmod(bc_num num1, bc_num num2, bc_num *quot, bc_num *rem, size_t scale)
44+
bool bc_divmod(bc_num num1, bc_num num2, bc_num *quot, bc_num *rem, size_t scale)
4245
{
4346
bc_num quotient = NULL;
4447
bc_num temp;
4548
size_t rscale;
4649

47-
/* Check for correct numbers. */
48-
if (bc_is_zero (num2)) {
49-
return -1;
50+
/* Cannot divide/mod by 0. */
51+
if (bc_is_zero(num2)) {
52+
return false;
5053
}
5154

5255
/* Calculate final scale. */
@@ -67,14 +70,14 @@ int bc_divmod(bc_num num1, bc_num num2, bc_num *quot, bc_num *rem, size_t scale)
6770
*quot = quotient;
6871
}
6972

70-
return 0; /* Everything is OK. */
73+
return true;
7174
}
7275

7376

7477
/* Modulo for numbers. This computes NUM1 % NUM2 and puts the
7578
result in RESULT. */
7679

77-
int bc_modulo(bc_num num1, bc_num num2, bc_num *result, size_t scale)
80+
bool bc_modulo(bc_num num1, bc_num num2, bc_num *result, size_t scale)
7881
{
7982
return bc_divmod(num1, num2, NULL, result, scale);
8083
}

0 commit comments

Comments
 (0)