mirror of
https://github.com/php/php-src.git
synced 2024-09-22 10:27:25 +00:00
Add the ability to take the logarithm of any base by adding a base parameter
to log() Added regression tests for the new form
This commit is contained in:
parent
9e2a312f2e
commit
84bd2901c4
@ -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;
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user