Skip to content

use log2() / log10() within basic log function #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/standard/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ fi
dnl
dnl Check for available functions
dnl
AC_CHECK_FUNCS(getcwd getwd asinh acosh atanh log1p hypot glob strfmon nice fpclass isinf isnan mempcpy strpncpy)
AC_CHECK_FUNCS(getcwd getwd asinh acosh atanh log1p log2 hypot glob strfmon nice fpclass isinf isnan mempcpy strpncpy)
AC_FUNC_FNMATCH

dnl
Expand Down
23 changes: 17 additions & 6 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,15 +704,26 @@ PHP_FUNCTION(log)
if (ZEND_NUM_ARGS() == 1) {
RETURN_DOUBLE(log(num));
}
if (base <= 0.0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "base must be greater than 0");
RETURN_FALSE;

#ifdef HAVE_LOG2
if (base == 2.0) {
RETURN_DOUBLE(log2(num));
}
if (base == 1) {
#endif
if (base == 10.0) {
RETURN_DOUBLE(log10(num));
}

if (base == 1.0) {
RETURN_DOUBLE(php_get_nan());
} else {
RETURN_DOUBLE(log(num) / log(base));
}

if (base <= 0.0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "base must be greater than 0");
RETURN_FALSE;
}

RETURN_DOUBLE(log(num) / log(base));
}
/* }}} */

Expand Down