mirror of
https://github.com/php/php-src.git
synced 2025-08-18 23:18:56 +02:00
Refactor bcmath
This commit is contained in:
parent
7e3989f301
commit
fee9d4cafd
3 changed files with 40 additions and 55 deletions
|
@ -230,9 +230,7 @@ PHP_FUNCTION(bcadd)
|
||||||
result->n_scale = scale;
|
result->n_scale = scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
Z_STRVAL_P(return_value) = bc_num2str(result);
|
RETVAL_STR(bc_num2str(result));
|
||||||
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
||||||
Z_TYPE_P(return_value) = IS_STRING;
|
|
||||||
bc_free_num(&first);
|
bc_free_num(&first);
|
||||||
bc_free_num(&second);
|
bc_free_num(&second);
|
||||||
bc_free_num(&result);
|
bc_free_num(&result);
|
||||||
|
@ -269,9 +267,7 @@ PHP_FUNCTION(bcsub)
|
||||||
result->n_scale = scale;
|
result->n_scale = scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
Z_STRVAL_P(return_value) = bc_num2str(result);
|
RETVAL_STR(bc_num2str(result));
|
||||||
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
||||||
Z_TYPE_P(return_value) = IS_STRING;
|
|
||||||
bc_free_num(&first);
|
bc_free_num(&first);
|
||||||
bc_free_num(&second);
|
bc_free_num(&second);
|
||||||
bc_free_num(&result);
|
bc_free_num(&result);
|
||||||
|
@ -308,9 +304,7 @@ PHP_FUNCTION(bcmul)
|
||||||
result->n_scale = scale;
|
result->n_scale = scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
Z_STRVAL_P(return_value) = bc_num2str(result);
|
RETVAL_STR(bc_num2str(result));
|
||||||
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
||||||
Z_TYPE_P(return_value) = IS_STRING;
|
|
||||||
bc_free_num(&first);
|
bc_free_num(&first);
|
||||||
bc_free_num(&second);
|
bc_free_num(&second);
|
||||||
bc_free_num(&result);
|
bc_free_num(&result);
|
||||||
|
@ -347,9 +341,7 @@ PHP_FUNCTION(bcdiv)
|
||||||
if (result->n_scale > scale) {
|
if (result->n_scale > scale) {
|
||||||
result->n_scale = scale;
|
result->n_scale = scale;
|
||||||
}
|
}
|
||||||
Z_STRVAL_P(return_value) = bc_num2str(result);
|
RETVAL_STR(bc_num2str(result));
|
||||||
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
||||||
Z_TYPE_P(return_value) = IS_STRING;
|
|
||||||
break;
|
break;
|
||||||
case -1: /* division by zero */
|
case -1: /* division by zero */
|
||||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Division by zero");
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Division by zero");
|
||||||
|
@ -383,9 +375,7 @@ PHP_FUNCTION(bcmod)
|
||||||
|
|
||||||
switch (bc_modulo(first, second, &result, 0 TSRMLS_CC)) {
|
switch (bc_modulo(first, second, &result, 0 TSRMLS_CC)) {
|
||||||
case 0:
|
case 0:
|
||||||
Z_STRVAL_P(return_value) = bc_num2str(result);
|
RETVAL_STR(bc_num2str(result));
|
||||||
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
||||||
Z_TYPE_P(return_value) = IS_STRING;
|
|
||||||
break;
|
break;
|
||||||
case -1:
|
case -1:
|
||||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Division by zero");
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Division by zero");
|
||||||
|
@ -427,9 +417,7 @@ PHP_FUNCTION(bcpowmod)
|
||||||
if (result->n_scale > scale) {
|
if (result->n_scale > scale) {
|
||||||
result->n_scale = scale;
|
result->n_scale = scale;
|
||||||
}
|
}
|
||||||
Z_STRVAL_P(return_value) = bc_num2str(result);
|
RETVAL_STR(bc_num2str(result));
|
||||||
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
||||||
Z_TYPE_P(return_value) = IS_STRING;
|
|
||||||
} else {
|
} else {
|
||||||
RETVAL_FALSE;
|
RETVAL_FALSE;
|
||||||
}
|
}
|
||||||
|
@ -471,9 +459,7 @@ PHP_FUNCTION(bcpow)
|
||||||
result->n_scale = scale;
|
result->n_scale = scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
Z_STRVAL_P(return_value) = bc_num2str(result);
|
RETVAL_STR(bc_num2str(result));
|
||||||
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
||||||
Z_TYPE_P(return_value) = IS_STRING;
|
|
||||||
bc_free_num(&first);
|
bc_free_num(&first);
|
||||||
bc_free_num(&second);
|
bc_free_num(&second);
|
||||||
bc_free_num(&result);
|
bc_free_num(&result);
|
||||||
|
@ -506,9 +492,7 @@ PHP_FUNCTION(bcsqrt)
|
||||||
if (result->n_scale > scale) {
|
if (result->n_scale > scale) {
|
||||||
result->n_scale = scale;
|
result->n_scale = scale;
|
||||||
}
|
}
|
||||||
Z_STRVAL_P(return_value) = bc_num2str(result);
|
RETVAL_STR(bc_num2str(result));
|
||||||
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
||||||
Z_TYPE_P(return_value) = IS_STRING;
|
|
||||||
} else {
|
} else {
|
||||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Square root of negative number");
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Square root of negative number");
|
||||||
}
|
}
|
||||||
|
@ -541,8 +525,7 @@ PHP_FUNCTION(bccomp)
|
||||||
|
|
||||||
bc_str2num(&first, left, scale TSRMLS_CC);
|
bc_str2num(&first, left, scale TSRMLS_CC);
|
||||||
bc_str2num(&second, right, scale TSRMLS_CC);
|
bc_str2num(&second, right, scale TSRMLS_CC);
|
||||||
Z_LVAL_P(return_value) = bc_compare(first, second);
|
RETVAL_LONG(bc_compare(first, second));
|
||||||
Z_TYPE_P(return_value) = IS_LONG;
|
|
||||||
|
|
||||||
bc_free_num(&first);
|
bc_free_num(&first);
|
||||||
bc_free_num(&second);
|
bc_free_num(&second);
|
||||||
|
|
|
@ -111,7 +111,7 @@ _PROTOTYPE(void bc_init_num, (bc_num *num TSRMLS_DC));
|
||||||
|
|
||||||
_PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale TSRMLS_DC));
|
_PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale TSRMLS_DC));
|
||||||
|
|
||||||
_PROTOTYPE(char *bc_num2str, (bc_num num));
|
_PROTOTYPE(zend_string *bc_num2str, (bc_num num));
|
||||||
|
|
||||||
_PROTOTYPE(void bc_int2num, (bc_num *num, int val));
|
_PROTOTYPE(void bc_int2num, (bc_num *num, int val));
|
||||||
|
|
||||||
|
|
|
@ -40,40 +40,42 @@
|
||||||
|
|
||||||
/* Convert a numbers to a string. Base 10 only.*/
|
/* Convert a numbers to a string. Base 10 only.*/
|
||||||
|
|
||||||
char
|
zend_string
|
||||||
*bc_num2str (num)
|
*bc_num2str (num)
|
||||||
bc_num num;
|
bc_num num;
|
||||||
{
|
{
|
||||||
char *str, *sptr;
|
zend_string *str;
|
||||||
char *nptr;
|
char *sptr;
|
||||||
int index, signch;
|
char *nptr;
|
||||||
|
int index, signch;
|
||||||
|
|
||||||
/* Allocate the string memory. */
|
/* Allocate the string memory. */
|
||||||
signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */
|
signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */
|
||||||
if (num->n_scale > 0)
|
if (num->n_scale > 0)
|
||||||
str = (char *) safe_emalloc (1, num->n_len + num->n_scale, 2 + signch);
|
str = STR_ALLOC(num->n_len + num->n_scale + signch + 1, 0);
|
||||||
else
|
else
|
||||||
str = (char *) safe_emalloc (1, num->n_len, 1 + signch);
|
str = STR_ALLOC(num->n_len + signch, 0);
|
||||||
if (str == NULL) bc_out_of_memory();
|
if (str == NULL) bc_out_of_memory();
|
||||||
|
|
||||||
/* The negative sign if needed. */
|
/* The negative sign if needed. */
|
||||||
sptr = str;
|
sptr = str->val;
|
||||||
if (signch) *sptr++ = '-';
|
if (signch) *sptr++ = '-';
|
||||||
|
|
||||||
/* Load the whole number. */
|
/* Load the whole number. */
|
||||||
nptr = num->n_value;
|
nptr = num->n_value;
|
||||||
for (index=num->n_len; index>0; index--)
|
for (index=num->n_len; index>0; index--)
|
||||||
*sptr++ = BCD_CHAR(*nptr++);
|
*sptr++ = BCD_CHAR(*nptr++);
|
||||||
|
|
||||||
/* Now the fraction. */
|
/* Now the fraction. */
|
||||||
if (num->n_scale > 0)
|
if (num->n_scale > 0)
|
||||||
{
|
{
|
||||||
*sptr++ = '.';
|
*sptr++ = '.';
|
||||||
for (index=0; index<num->n_scale; index++)
|
for (index=0; index<num->n_scale; index++)
|
||||||
*sptr++ = BCD_CHAR(*nptr++);
|
*sptr++ = BCD_CHAR(*nptr++);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Terminate the string and return it! */
|
/* Terminate the string and return it! */
|
||||||
*sptr = '\0';
|
*sptr = '\0';
|
||||||
return (str);
|
str->len = sptr - (char *)str->val;
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue