diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index a40fdd23979f1..98a373903e336 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1729,6 +1729,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_log, 0, 0, 1) ZEND_ARG_INFO(0, base) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO(arginfo_log2, 0) + ZEND_ARG_INFO(0, number) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO(arginfo_log10, 0) ZEND_ARG_INFO(0, number) ZEND_END_ARG_INFO() @@ -2916,6 +2920,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(pow, arginfo_pow) PHP_FE(exp, arginfo_exp) PHP_FE(log, arginfo_log) + PHP_FE(log2, arginfo_log2) PHP_FE(log10, arginfo_log10) PHP_FE(sqrt, arginfo_sqrt) PHP_FE(hypot, arginfo_hypot) diff --git a/ext/standard/math.c b/ext/standard/math.c index be2d655263f13..1077e58d871e4 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -716,6 +716,19 @@ PHP_FUNCTION(log) } /* }}} */ +/* {{{ proto float log2(float number) + Returns the base-2 logarithm of the number */ +PHP_FUNCTION(log2) +{ + double num; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) { + return; + } + RETURN_DOUBLE(log2(num)); +} +/* }}} */ + /* {{{ proto float log10(float number) Returns the base-10 logarithm of the number */ PHP_FUNCTION(log10) diff --git a/ext/standard/php_math.h b/ext/standard/php_math.h index 116acd9df5e8b..bcc49693f751d 100644 --- a/ext/standard/php_math.h +++ b/ext/standard/php_math.h @@ -39,6 +39,7 @@ PHP_FUNCTION(atan2); PHP_FUNCTION(pi); PHP_FUNCTION(exp); PHP_FUNCTION(log); +PHP_FUNCTION(log2); PHP_FUNCTION(log10); PHP_FUNCTION(is_finite); PHP_FUNCTION(is_infinite); diff --git a/ext/standard/tests/math/log2_basic.phpt b/ext/standard/tests/math/log2_basic.phpt new file mode 100644 index 0000000000000..f2acf272beb2a --- /dev/null +++ b/ext/standard/tests/math/log2_basic.phpt @@ -0,0 +1,52 @@ +--TEST-- +Test return type and value for expected input log2() +--INI-- +precision = 14 +--FILE-- + +--EXPECTF-- +log2 1 = float(%f) +Pass +log2 10 = float(%f) +Pass +log2 100 = float(%f) +Pass diff --git a/ext/standard/tests/math/log2_basiclong_64bit.phpt b/ext/standard/tests/math/log2_basiclong_64bit.phpt new file mode 100644 index 0000000000000..11f82f8add031 --- /dev/null +++ b/ext/standard/tests/math/log2_basiclong_64bit.phpt @@ -0,0 +1,60 @@ +--TEST-- +Test log2 function : 64bit long tests +--SKIPIF-- + +--FILE-- + +===DONE=== +--EXPECT-- +--- testing: 9223372036854775807 --- +float(63) +--- testing: -9223372036854775808 --- +float(NAN) +--- testing: 2147483647 --- +float(30.999999999328) +--- testing: -2147483648 --- +float(NAN) +--- testing: 9223372034707292160 --- +float(62.999999999664) +--- testing: -9223372034707292160 --- +float(NAN) +--- testing: 2147483648 --- +float(31) +--- testing: -2147483649 --- +float(NAN) +--- testing: 4294967294 --- +float(31.999999999328) +--- testing: 4294967295 --- +float(31.999999999664) +--- testing: 4294967293 --- +float(31.999999998992) +--- testing: 9223372036854775806 --- +float(63) +--- testing: 9.2233720368548E+18 --- +float(63) +--- testing: -9223372036854775807 --- +float(NAN) +--- testing: -9.2233720368548E+18 --- +float(NAN) +===DONE=== diff --git a/ext/standard/tests/math/log2_error.phpt b/ext/standard/tests/math/log2_error.phpt new file mode 100644 index 0000000000000..6ca8f96f37d11 --- /dev/null +++ b/ext/standard/tests/math/log2_error.phpt @@ -0,0 +1,31 @@ +--TEST-- +Test wrong number of arguments for log2() +--INI-- +precision = 14 +--FILE-- + +--EXPECTF-- +Too many arguments + +Warning: log2() expects exactly 1 parameter, 2 given in %s on line %d +NULL + +Too few arguments + +Warning: log2() expects exactly 1 parameter, 0 given in %s on line %d +NULL diff --git a/ext/standard/tests/math/log2_variation.phpt b/ext/standard/tests/math/log2_variation.phpt new file mode 100644 index 0000000000000..aebde8fa7bf4f --- /dev/null +++ b/ext/standard/tests/math/log2_variation.phpt @@ -0,0 +1,56 @@ +--TEST-- +Test variations in usage of log2() +--INI-- +precision = 10 +--FILE-- + +--EXPECTF-- +float(4.523561956) +float(NAN) +float(4.551516018) +float(NAN) +float(4.523561956) +float(4.523561956) +float(4.523561956) +float(4.551516018) +float(4.551516018) + +Warning: log2() expects parameter 1 to be double, string given in %s on line %d +NULL +float(9.965784285) + +Notice: A non well formed numeric value encountered in %s on line %d +float(9.965784285) +float(-INF) +float(0) +float(-INF)