summaryrefslogtreecommitdiff
path: root/ext/bcmath/bcmath.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bcmath/bcmath.c')
-rw-r--r--ext/bcmath/bcmath.c61
1 files changed, 7 insertions, 54 deletions
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index 8b3e3d9514..1186241620 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -207,21 +207,6 @@ static void php_str2num(bc_num *num, char *str)
}
/* }}} */
-/* {{{ split_bc_num
- Convert to bc_num detecting scale */
-static bc_num split_bc_num(bc_num num) {
- bc_num newnum;
- if (num->n_refs >= 1) {
- return num;
- }
- newnum = _bc_new_num_ex(0, 0, 0);
- *newnum = *num;
- newnum->n_refs = 1;
- num->n_refs--;
- return newnum;
-}
-/* }}} */
-
/* {{{ proto string bcadd(string left_operand, string right_operand [, int scale])
Returns the sum of two arbitrary precision numbers */
PHP_FUNCTION(bcadd)
@@ -249,12 +234,7 @@ PHP_FUNCTION(bcadd)
php_str2num(&second, ZSTR_VAL(right));
bc_add (first, second, &result, scale);
- if (result->n_scale > scale) {
- result = split_bc_num(result);
- result->n_scale = scale;
- }
-
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
@@ -289,12 +269,7 @@ PHP_FUNCTION(bcsub)
php_str2num(&second, ZSTR_VAL(right));
bc_sub (first, second, &result, scale);
- if (result->n_scale > scale) {
- result = split_bc_num(result);
- result->n_scale = scale;
- }
-
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
@@ -329,12 +304,7 @@ PHP_FUNCTION(bcmul)
php_str2num(&second, ZSTR_VAL(right));
bc_multiply (first, second, &result, scale);
- if (result->n_scale > scale) {
- result = split_bc_num(result);
- result->n_scale = scale;
- }
-
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
@@ -370,11 +340,7 @@ PHP_FUNCTION(bcdiv)
switch (bc_divide(first, second, &result, scale)) {
case 0: /* OK */
- if (result->n_scale > scale) {
- result = split_bc_num(result);
- result->n_scale = scale;
- }
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale));
break;
case -1: /* division by zero */
php_error_docref(NULL, E_WARNING, "Division by zero");
@@ -450,11 +416,7 @@ PHP_FUNCTION(bcpowmod)
scale_int = (int) ((int)scale < 0 ? 0 : scale);
if (bc_raisemod(first, second, mod, &result, scale_int) != -1) {
- if (result->n_scale > scale_int) {
- result = split_bc_num(result);
- result->n_scale = scale_int;
- }
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale_int));
} else {
RETVAL_FALSE;
}
@@ -494,12 +456,7 @@ PHP_FUNCTION(bcpow)
php_str2num(&second, ZSTR_VAL(right));
bc_raise (first, second, &result, scale);
- if (result->n_scale > scale) {
- result = split_bc_num(result);
- result->n_scale = scale;
- }
-
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
@@ -530,11 +487,7 @@ PHP_FUNCTION(bcsqrt)
php_str2num(&result, ZSTR_VAL(left));
if (bc_sqrt (&result, scale) != 0) {
- if (result->n_scale > scale) {
- result = split_bc_num(result);
- result->n_scale = scale;
- }
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale));
} else {
php_error_docref(NULL, E_WARNING, "Square root of negative number");
}