From 7a4eb25870e48661e1b356b71cac3297550a1758 Mon Sep 17 00:00:00 2001 From: Andrey Hristov Date: Mon, 2 May 2005 12:12:04 +0000 Subject: [PATCH] add also math_variance() which uses the same calculation as math_std_dev() --- NEWS | 1 + ext/standard/basic_functions.c | 1 + ext/standard/math.c | 49 +++++++++++++++++------ ext/standard/php_math.h | 1 + ext/standard/tests/math/math_std_dev.phpt | 9 ++++- 5 files changed, 46 insertions(+), 15 deletions(-) diff --git a/NEWS b/NEWS index 9a4e3ad52c..487b5099fa 100644 --- a/NEWS +++ b/NEWS @@ -92,6 +92,7 @@ PHP NEWS . htmlspecialchars_decode() (Ilia) . time_sleep_until() (Ilia) . math_std_dev() (Andrey) + . math_variance() - calculating population variance (Andrey) - Added DomDocument::$recover property for parsing not well-formed XML Documents. (Christian) - Added Cursor support for MySQL 5.0.x in mysqli (Georg) 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-- --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 -- 2.40.0