summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/standard/math.c34
-rw-r--r--ext/standard/tests/math/log.phpt29
2 files changed, 51 insertions, 12 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;
}
/* }}} */
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--
<?php // $Id$
echo "On failure, please mail result to php-dev@lists.php.net\n";
-for ($x=0, $count=0; $x < 200; $x++) {
+for ($x = 0, $count= 0; $x < 200; $x++) {
$x2 = (int) exp(log($x));
// e ^ log(x) should be close in range to x
if (($x2 < ($x + 2)) && ($x2 > ($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