]> granicus.if.org Git - php/commitdiff
add also math_variance() which uses the same calculation as math_std_dev()
authorAndrey Hristov <andrey@php.net>
Mon, 2 May 2005 12:12:04 +0000 (12:12 +0000)
committerAndrey Hristov <andrey@php.net>
Mon, 2 May 2005 12:12:04 +0000 (12:12 +0000)
NEWS
ext/standard/basic_functions.c
ext/standard/math.c
ext/standard/php_math.h
ext/standard/tests/math/math_std_dev.phpt

diff --git a/NEWS b/NEWS
index 9a4e3ad52c4e9f32bd21117e4170825e4d1f2e3c..487b5099fa932dea0dd1952b5d2a32f8d0433108 100644 (file)
--- 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)
index 3c117626340bba24b22deeb48fdfb54265c026d1..047982d43bf69db08bb61dbfcdd03d7c188afb4c 100644 (file)
@@ -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
index 528b864cdd9a1c196b804d46880699548b005fed..3e27103e3c1a07f2c0ab847946735efbb4b38a25 100644 (file)
@@ -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)));
 }
 /* }}} */
 
index 33efb0b9b208e232e6f82ea20a3adf084aae2b13..385f34b142dae6cb881cea19127affa989d23b92 100644 (file)
@@ -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);
 
index a8f74278fbadc7cd7188d8e45c5daee6ac233aee..6c9f9694acd2d1fb19dbc7e26493dfb868aacd41 100644 (file)
@@ -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