From: Jason Greene Date: Mon, 11 Nov 2002 05:21:35 +0000 (+0000) Subject: Add the ability to take the logarithm of any base by adding a base parameter X-Git-Tag: php-4.3.0RC1~123 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=84bd2901c4b41b3b4ef9fa730f51e3b30bf6a0e4;p=php Add the ability to take the logarithm of any base by adding a base parameter to log() Added regression tests for the new form --- 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; } /* }}} */ diff --git a/ext/standard/tests/math/log.phpt b/ext/standard/tests/math/log.phpt index 344c72fa0d..285b19c853 100644 --- a/ext/standard/tests/math/log.phpt +++ b/ext/standard/tests/math/log.phpt @@ -5,17 +5,40 @@ log() tests --FILE-- ($x - 2))) { $count++; - } - else { + } else { print "$x : $x2\n"; } } print $count . "\n"; + +// Now test the base form of log +for ($base = 2; $base < 11; $base++) { + for ($x = 0, $count= 0; $x < 50; $x++) { + $x2 = (int) pow($base, log($x, $base)); + // base ^ log(x) should be close in range to x + if (($x2 < ($x + 2)) && ($x2 > ($x - 2))) { + $count++; + } else { + print "base $base: $x : $x2\n"; + } + } + print $count . "\n"; +} +?> --EXPECT-- On failure, please mail result to php-dev@lists.php.net 200 +50 +50 +50 +50 +50 +50 +50 +50 +50