From 870ed5106d6274905b5aa4341429deef12c92e55 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Fri, 8 Sep 2017 18:31:04 +0200 Subject: Fixed bug #66364 (BCMath bcmul ignores scale parameter) We change `bcmul()` and `bcpow()` so that the result has exactly the requested scale (i.e. decimal places) to make them consistent with the other BCMath functions. This also changes our stance regarding bug #52748, which had been classified as documentation problem. We do not manipulate the numbers themselves (anymore), but rather introduce `bc_num2str_ex()` which accepts a scale parameter that overrides the scale of the number by omitting extraneous decimals and adding zeros, respectively. This also allows us to get rid of `split_bc_num()`, which fixes bug #75164 as well. --- ext/bcmath/libbcmath/src/bcmath.h | 3 ++- ext/bcmath/libbcmath/src/num2str.c | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'ext/bcmath/libbcmath/src') diff --git a/ext/bcmath/libbcmath/src/bcmath.h b/ext/bcmath/libbcmath/src/bcmath.h index b83ba13558..08c548063e 100644 --- a/ext/bcmath/libbcmath/src/bcmath.h +++ b/ext/bcmath/libbcmath/src/bcmath.h @@ -110,7 +110,7 @@ _PROTOTYPE(void bc_init_num, (bc_num *num)); _PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale)); -_PROTOTYPE(zend_string *bc_num2str, (bc_num num)); +_PROTOTYPE(zend_string *bc_num2str_ex, (bc_num num, int scale)); _PROTOTYPE(void bc_int2num, (bc_num *num, int val)); @@ -157,5 +157,6 @@ _PROTOTYPE(void bc_out_of_memory, (void)); #define bc_new_num(length, scale) _bc_new_num_ex((length), (scale), 0) #define bc_free_num(num) _bc_free_num_ex((num), 0) +#define bc_num2str(num) bc_num2str_ex((num), (num->n_scale)) #endif diff --git a/ext/bcmath/libbcmath/src/num2str.c b/ext/bcmath/libbcmath/src/num2str.c index c72a924933..7316d32a14 100644 --- a/ext/bcmath/libbcmath/src/num2str.c +++ b/ext/bcmath/libbcmath/src/num2str.c @@ -41,8 +41,9 @@ /* Convert a numbers to a string. Base 10 only.*/ zend_string -*bc_num2str (num) +*bc_num2str_ex (num, scale) bc_num num; + int scale; { zend_string *str; char *sptr; @@ -51,8 +52,8 @@ zend_string /* Allocate the string memory. */ signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */ - if (num->n_scale > 0) - str = zend_string_alloc(num->n_len + num->n_scale + signch + 1, 0); + if (scale > 0) + str = zend_string_alloc(num->n_len + scale + signch + 1, 0); else str = zend_string_alloc(num->n_len + signch, 0); if (str == NULL) bc_out_of_memory(); @@ -67,11 +68,13 @@ zend_string *sptr++ = BCD_CHAR(*nptr++); /* Now the fraction. */ - if (num->n_scale > 0) + if (scale > 0) { *sptr++ = '.'; - for (index=0; indexn_scale; index++) + for (index=0; indexn_scale; index++) *sptr++ = BCD_CHAR(*nptr++); + for (index = num->n_scale; index