Skip to content

Feature/log2 #337

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 4 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
5 changes: 5 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
52 changes: 52 additions & 0 deletions ext/standard/tests/math/log2_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
Test return type and value for expected input log2()
--INI--
precision = 14
--FILE--
<?php
/*
* proto float log2(float number)
* Function is implemented in ext/standard/math.c
*/

$file_path = dirname(__FILE__);
require($file_path."/allowed_rounding_error.inc");

$arg_0 = 1.0;
$arg_1 = 10.0;
$arg_2 = 100.0;

echo "log2 $arg_0 = ";
$r0 = log2($arg_0);
var_dump($r0);
if (allowed_rounding_error($r0 ,0.0 )) {
echo "Pass\n";
} else {
echo "Fail\n";
}

echo "log2 $arg_1 = ";
$r1 = log2($arg_1);
var_dump($r1);
if (allowed_rounding_error($r1, 3.3219280948874)) {
echo "Pass\n";
} else {
echo "Fail\n";
}

echo "log2 $arg_2 = ";
$r2 = log2($arg_2);
var_dump($r2);
if (allowed_rounding_error($r2, 6.6438561897747)) {
echo "Pass\n";
} else {
echo "Fail\n";
}
?>
--EXPECTF--
log2 1 = float(%f)
Pass
log2 10 = float(%f)
Pass
log2 100 = float(%f)
Pass
60 changes: 60 additions & 0 deletions ext/standard/tests/math/log2_basiclong_64bit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
Test log2 function : 64bit long tests
--SKIPIF--
<?php
if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only");
?>
--FILE--
<?php

define("MAX_64Bit", 9223372036854775807);
define("MAX_32Bit", 2147483647);
define("MIN_64Bit", -9223372036854775807 - 1);
define("MIN_32Bit", -2147483647 - 1);

$longVals = array(
MAX_64Bit, MIN_64Bit, MAX_32Bit, MIN_32Bit, MAX_64Bit - MAX_32Bit, MIN_64Bit - MIN_32Bit,
MAX_32Bit + 1, MIN_32Bit - 1, MAX_32Bit * 2, (MAX_32Bit * 2) + 1, (MAX_32Bit * 2) - 1,
MAX_64Bit -1, MAX_64Bit + 1, MIN_64Bit + 1, MIN_64Bit - 1
);


foreach ($longVals as $longVal) {
echo "--- testing: $longVal ---\n";
var_dump(log2($longVal));
}

?>
===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===
31 changes: 31 additions & 0 deletions ext/standard/tests/math/log2_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
Test wrong number of arguments for log2()
--INI--
precision = 14
--FILE--
<?php
/*
* proto float log2(float number)
* Function is implemented in ext/standard/math.c
*/

$arg_0 = 1.0;
$extra_arg = 1;

echo "\nToo many arguments\n";
var_dump(log2($arg_0, $extra_arg));

echo "\nToo few arguments\n";
var_dump(log2());

?>
--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
56 changes: 56 additions & 0 deletions ext/standard/tests/math/log2_variation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
Test variations in usage of log2()
--INI--
precision = 10
--FILE--
<?php
/*
* proto float log2(float number)
* Function is implemented in ext/standard/math.c
*/


//Test log2 with a different input values

$values = array(23,
-23,
2.345e1,
-2.345e1,
0x17,
027,
"23",
"23.45",
"2.345e1",
"nonsense",
"1000",
"1000ABC",
null,
true,
false);

for ($i = 0; $i < count($values); $i++) {
$res = log2($values[$i]);
var_dump($res);
}

?>
--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)