summaryrefslogtreecommitdiff
path: root/ext/standard/math.c
diff options
context:
space:
mode:
authorJason Greene <jason@php.net>2002-11-11 05:21:35 +0000
committerJason Greene <jason@php.net>2002-11-11 05:21:35 +0000
commit84bd2901c4b41b3b4ef9fa730f51e3b30bf6a0e4 (patch)
tree2fee59988076bb6e82442ab1bf2181d2b4df4750 /ext/standard/math.c
parent9e2a312f2e5ef9619245bdf42f920c406cbf7d65 (diff)
downloadphp-git-84bd2901c4b41b3b4ef9fa730f51e3b30bf6a0e4.tar.gz
Add the ability to take the logarithm of any base by adding a base parameter
to log() Added regression tests for the new form
Diffstat (limited to 'ext/standard/math.c')
-rw-r--r--ext/standard/math.c34
1 files changed, 25 insertions, 9 deletions
diff --git a/ext/standard/math.c b/ext/standard/math.c
index dafaf05788..9be11538c6 100644
--- a/ext/standard/math.c
+++ b/ext/standard/math.c
@@ -520,19 +520,35 @@ PHP_FUNCTION(log1p)
/* }}} */
#endif
-/* {{{ proto float log(float number)
- Returns the natural logarithm of the number */
+/* {{{ proto float log(float number, [float base])
+ Returns the natural logarithm of the number, or the base log if base is specified */
PHP_FUNCTION(log)
{
- zval **num;
-
- if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
- WRONG_PARAM_COUNT;
+ zval **num, **base;
+
+ switch (ZEND_NUM_ARGS()) {
+ case 1:
+ if (zend_get_parameters_ex(1, &num) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_double_ex(num);
+ RETURN_DOUBLE(log(Z_DVAL_PP(num)));
+ case 2:
+ if (zend_get_parameters_ex(2, &num, &base) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_double_ex(num);
+ convert_to_double_ex(base);
+
+ if (Z_DVAL_PP(base) <= 0.0) {
+ php_error(E_WARNING, "base must be greater than 0", Z_DVAL_PP(base));
+ RETURN_FALSE;
+ }
+ RETURN_DOUBLE(log(Z_DVAL_PP(num)) / log(Z_DVAL_PP(base)));
+ default:
+ WRONG_PARAM_COUNT;
}
- convert_to_double_ex(num);
- Z_DVAL_P(return_value) = log(Z_DVAL_PP(num));
- Z_TYPE_P(return_value) = IS_DOUBLE;
}
/* }}} */