Re: cvs: php-src /ext/standard basic_functions.c math.c php_math.h /ext/standard/tests/math math_std_dev.phpt

From: Date: Mon, 02 May 2005 10:49:49 +0000
Subject: Re: cvs: php-src /ext/standard basic_functions.c math.c php_math.h /ext/standard/tests/math math_std_dev.phpt
References: 1 2  Groups: php.cvs 
Request: Send a blank email to php-cvs+get-31701@lists.php.net to get a copy of this message
Good catch! Stefan Esser wrote:
Why not catching division by zero? Stefan Andrey Hristov wrote:
andrey        Mon May  2 05:17:50 2005 EDT
  Added files:                     /php-src/ext/standard/tests/math    math_std_dev.phpt
  Modified files:                  /php-src/ext/standard    basic_functions.c math.c php_math.h   Log:
add math_std_dev()
    http://cvs.php.net/diff.php/php-src/ext/standard/basic_functions.c?r1=1.711&r2=1.712&ty=u 
Index: php-src/ext/standard/basic_functions.c diff -u php-src/ext/standard/basic_functions.c:1.711 php-src/ext/standard/basic_functions.c:1.712
--- php-src/ext/standard/basic_functions.c:1.711    Wed Apr 27 11:45:36 2005
+++ php-src/ext/standard/basic_functions.c    Mon May  2 05:17:49 2005
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
*/ -/* $Id: basic_functions.c,v 1.711 2005/04/27 15:45:36 dmitry Exp $ */ +/* $Id: basic_functions.c,v 1.712 2005/05/02 09:17:49 andrey Exp $ */ #include "php.h" #include "php_streams.h" @@ -415,6 +415,7 @@
     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 http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.119&r2=1.120&ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.119 php-src/ext/standard/math.c:1.120
--- php-src/ext/standard/math.c:1.119    Mon Dec 13 19:37:19 2004
+++ php-src/ext/standard/math.c    Mon May  2 05:17:49 2005
@@ -19,7 +19,7 @@
    +----------------------------------------------------------------------+
*/ -/* $Id: math.c,v 1.119 2004/12/14 00:37:19 iliaa Exp $ */ +/* $Id: math.c,v 1.120 2005/05/02 09:17:49 andrey Exp $ */ #include "php.h" #include "php_math.h" @@ -1182,6 +1182,40 @@ } /* }}} */ + + +/* {{{ 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 http://cvs.php.net/diff.php/php-src/ext/standard/php_math.h?r1=1.23&r2=1.24&ty=u Index: php-src/ext/standard/php_math.h diff -u php-src/ext/standard/php_math.h:1.23 php-src/ext/standard/php_math.h:1.24
--- php-src/ext/standard/php_math.h:1.23    Tue Sep 21 14:09:29 2004
+++ php-src/ext/standard/php_math.h    Mon May  2 05:17:49 2005
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
*/ -/* $Id: php_math.h,v 1.23 2004/09/21 18:09:29 fmk Exp $ */ +/* $Id: php_math.h,v 1.24 2005/05/02 09:17:49 andrey Exp $ */ #ifndef PHP_MATH_H #define PHP_MATH_H @@ -59,6 +59,7 @@ PHP_FUNCTION(base_convert); PHP_FUNCTION(number_format); PHP_FUNCTION(fmod); +PHP_FUNCTION(math_std_dev); PHP_FUNCTION(deg2rad); PHP_FUNCTION(rad2deg); http://cvs.php.net/co.php/php-src/ext/standard/tests/math/math_std_dev.phpt?r=1.1&p=1 Index: php-src/ext/standard/tests/math/math_std_dev.phpt +++ php-src/ext/standard/tests/math/math_std_dev.phpt --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"


« previous php.cvs (#31701) next »