]> granicus.if.org Git - php/commitdiff
add math_std_dev()
authorAndrey Hristov <andrey@php.net>
Mon, 2 May 2005 09:17:49 +0000 (09:17 +0000)
committerAndrey Hristov <andrey@php.net>
Mon, 2 May 2005 09:17:49 +0000 (09:17 +0000)
ext/standard/basic_functions.c
ext/standard/math.c
ext/standard/php_math.h
ext/standard/tests/math/math_std_dev.phpt [new file with mode: 0644]

index fe9f781e84e03a93288402a52ecc1ddb6a48ab8e..3c117626340bba24b22deeb48fdfb54265c026d1 100644 (file)
@@ -415,6 +415,7 @@ function_entry basic_functions[] = {
        PHP_FE(base_convert,                                                                                                    NULL)
        PHP_FE(number_format,                                                                                                   NULL)
        PHP_FE(fmod,                                                                                                                    NULL)
+       PHP_FE(math_std_dev,                                                                                                    NULL)
 #ifdef HAVE_INET_NTOP
        PHP_NAMED_FE(inet_ntop,         php_inet_ntop,                                                                                  NULL)
 #endif
index 289c3c491b6906bf97e93273c619551fd8ffdb3c..a1237165fdd3cc2932ac5d3e9fa8d5eb3795a0af 100644 (file)
@@ -1182,6 +1182,40 @@ PHP_FUNCTION(fmod)
 }
 /* }}} */
 
+
+
+/* {{{ proto float math_std_dev(array a)
+   Returns the standard deviation */
+PHP_FUNCTION(math_std_dev)
+{
+       double mean, sum = 0.0, vr = 0.0;
+       zval *arr, **entry;
+       HashPosition pos;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a",  &arr) == FAILURE) {
+               return;
+       }
+       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);
+               sum += Z_DVAL_PP(entry);
+               zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);       
+       }
+       mean = sum / zend_hash_num_elements(Z_ARRVAL_P(arr));
+
+       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) {
+               double d;
+               convert_to_double_ex(entry);
+               d = Z_DVAL_PP(entry) - mean;
+               vr += d*d;
+               zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);       
+       }
+
+       RETURN_DOUBLE(sqrt(vr / zend_hash_num_elements(Z_ARRVAL_P(arr))));
+}
+/* }}} */
+
 /*
  * Local variables:
  * tab-width: 4
index adbc9f8cdf1556b6600be2f21218c1ddb5c18886..33efb0b9b208e232e6f82ea20a3adf084aae2b13 100644 (file)
@@ -59,6 +59,7 @@ PHP_FUNCTION(octdec);
 PHP_FUNCTION(base_convert);
 PHP_FUNCTION(number_format);
 PHP_FUNCTION(fmod);
+PHP_FUNCTION(math_std_dev);
 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
new file mode 100644 (file)
index 0000000..a68384c
--- /dev/null
@@ -0,0 +1,10 @@
+--TEST--
+math_std_dev() tests
+--FILE--
+<?php
+$a=array(4, 1, 7);
+$dev=math_std_dev($a);
+var_dump(sprintf("%2.9f", $dev));
+?>
+--EXPECT--
+string(11) "2.449489743"
\ No newline at end of file