diff options
author | Andrey Hristov <andrey@php.net> | 2005-05-02 12:12:04 +0000 |
---|---|---|
committer | Andrey Hristov <andrey@php.net> | 2005-05-02 12:12:04 +0000 |
commit | 7a4eb25870e48661e1b356b71cac3297550a1758 (patch) | |
tree | 84b53f3f850abf7715106cb33356ff0d23ea9918 /ext | |
parent | 6141c5690d251480852e185ed00f231e433cfef4 (diff) | |
download | php-git-7a4eb25870e48661e1b356b71cac3297550a1758.tar.gz |
add also math_variance() which uses the same calculation as math_std_dev()
Diffstat (limited to 'ext')
-rw-r--r-- | ext/standard/basic_functions.c | 1 | ||||
-rw-r--r-- | ext/standard/math.c | 49 | ||||
-rw-r--r-- | ext/standard/php_math.h | 1 | ||||
-rw-r--r-- | ext/standard/tests/math/math_std_dev.phpt | 9 |
4 files changed, 45 insertions, 15 deletions
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 3c11762634..047982d43b 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -416,6 +416,7 @@ function_entry basic_functions[] = { PHP_FE(number_format, NULL) PHP_FE(fmod, NULL) PHP_FE(math_std_dev, NULL) + PHP_FE(math_variance, NULL) #ifdef HAVE_INET_NTOP PHP_NAMED_FE(inet_ntop, php_inet_ntop, NULL) #endif diff --git a/ext/standard/math.c b/ext/standard/math.c index 528b864cdd..3e27103e3c 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -1183,22 +1183,12 @@ PHP_FUNCTION(fmod) /* }}} */ - -/* {{{ proto float math_std_dev(array a) - Returns the standard deviation */ -PHP_FUNCTION(math_std_dev) +static long double php_population_variance(zval *arr) { double mean, sum = 0.0, vr = 0.0; - zval *arr, **entry; + zval **entry; HashPosition pos; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) { - return; - } - if (zend_hash_num_elements(Z_ARRVAL_P(arr)) == 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "The array has zero elements"); - RETURN_FALSE; - } zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos); while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS) { convert_to_double_ex(entry); @@ -1215,8 +1205,41 @@ PHP_FUNCTION(math_std_dev) vr += d*d; zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos); } + return (vr / zend_hash_num_elements(Z_ARRVAL_P(arr))); +} + +/* {{{ proto float math_variance(array a) + Returns the standard deviation */ +PHP_FUNCTION(math_variance) +{ + zval *arr; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) { + return; + } + if (zend_hash_num_elements(Z_ARRVAL_P(arr)) == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "The array has zero elements"); + RETURN_FALSE; + } + RETURN_DOUBLE(php_population_variance(arr)); +} +/* }}} */ + - RETURN_DOUBLE(sqrt(vr / zend_hash_num_elements(Z_ARRVAL_P(arr)))); +/* {{{ proto float math_std_dev(array a) + Returns the standard deviation */ +PHP_FUNCTION(math_std_dev) +{ + zval *arr; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) { + return; + } + if (zend_hash_num_elements(Z_ARRVAL_P(arr)) == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "The array has zero elements"); + RETURN_FALSE; + } + RETURN_DOUBLE(sqrt(php_population_variance(arr))); } /* }}} */ diff --git a/ext/standard/php_math.h b/ext/standard/php_math.h index 33efb0b9b2..385f34b142 100644 --- a/ext/standard/php_math.h +++ b/ext/standard/php_math.h @@ -60,6 +60,7 @@ PHP_FUNCTION(base_convert); PHP_FUNCTION(number_format); PHP_FUNCTION(fmod); PHP_FUNCTION(math_std_dev); +PHP_FUNCTION(math_variance); PHP_FUNCTION(deg2rad); PHP_FUNCTION(rad2deg); diff --git a/ext/standard/tests/math/math_std_dev.phpt b/ext/standard/tests/math/math_std_dev.phpt index a8f74278fb..6c9f9694ac 100644 --- a/ext/standard/tests/math/math_std_dev.phpt +++ b/ext/standard/tests/math/math_std_dev.phpt @@ -1,14 +1,19 @@ --TEST-- -math_std_dev() tests +math_std_dev()/math_variance tests --FILE-- <?php $a=array(4, 1, 7); $dev=math_std_dev($a); var_dump(sprintf("%2.9f", $dev)); var_dump(math_std_dev(array())); +echo "---Variance---\n"; +$a=array(5,7,8,10,10); +var_dump(math_variance($a)); ?> --EXPECTF-- string(11) "2.449489743" Warning: math_std_dev(): The array has zero elements in %s on line %d -bool(false)
\ No newline at end of file +bool(false) +---Variance--- +float(3.6)
\ No newline at end of file |