cvs: php-src / NEWS /ext/standard basic_functions.c math.c php_math.h /ext/standard/tests/math math_std_dev.phpt
| From: | Andrey Hristov | Date: | Mon, 02 May 2005 12:12:04 +0000 |
| Subject: | cvs: php-src / NEWS /ext/standard basic_functions.c math.c php_math.h /ext/standard/tests/math math_std_dev.phpt | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-31703@lists.php.net to get a copy of this message | ||
andrey Mon May 2 08:12:04 2005 EDT
Modified files:
/php-src NEWS
/php-src/ext/standard basic_functions.c math.c php_math.h
/php-src/ext/standard/tests/math math_std_dev.phpt
Log:
add also math_variance() which uses the same calculation as math_std_dev()
http://cvs.php.net/diff.php/php-src/NEWS?r1=1.1886&r2=1.1887&ty=u
Index: php-src/NEWS
diff -u php-src/NEWS:1.1886 php-src/NEWS:1.1887
--- php-src/NEWS:1.1886 Mon May 2 05:18:05 2005
+++ php-src/NEWS Mon May 2 08:12:03 2005
@@ -92,6 +92,7 @@
. 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)
http://cvs.php.net/diff.php/php-src/ext/standard/basic_functions.c?r1=1.712&r2=1.713&ty=u
Index: php-src/ext/standard/basic_functions.c
diff -u php-src/ext/standard/basic_functions.c:1.712 php-src/ext/standard/basic_functions.c:1.713
--- php-src/ext/standard/basic_functions.c:1.712 Mon May 2 05:17:49 2005
+++ php-src/ext/standard/basic_functions.c Mon May 2 08:12:04 2005
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.712 2005/05/02 09:17:49 andrey Exp $ */
+/* $Id: basic_functions.c,v 1.713 2005/05/02 12:12:04 andrey Exp $ */
#include "php.h"
#include "php_streams.h"
@@ -416,6 +416,7 @@
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
http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.121&r2=1.122&ty=u
Index: php-src/ext/standard/math.c
diff -u php-src/ext/standard/math.c:1.121 php-src/ext/standard/math.c:1.122
--- php-src/ext/standard/math.c:1.121 Mon May 2 07:01:13 2005
+++ php-src/ext/standard/math.c Mon May 2 08:12:04 2005
@@ -19,7 +19,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: math.c,v 1.121 2005/05/02 11:01:13 andrey Exp $ */
+/* $Id: math.c,v 1.122 2005/05/02 12:12:04 andrey Exp $ */
#include "php.h"
#include "php_math.h"
@@ -1183,22 +1183,12 @@
/* }}} */
-
-/* {{{ 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 @@
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)));
}
/* }}} */
http://cvs.php.net/diff.php/php-src/ext/standard/php_math.h?r1=1.24&r2=1.25&ty=u
Index: php-src/ext/standard/php_math.h
diff -u php-src/ext/standard/php_math.h:1.24 php-src/ext/standard/php_math.h:1.25
--- php-src/ext/standard/php_math.h:1.24 Mon May 2 05:17:49 2005
+++ php-src/ext/standard/php_math.h Mon May 2 08:12:04 2005
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_math.h,v 1.24 2005/05/02 09:17:49 andrey Exp $ */
+/* $Id: php_math.h,v 1.25 2005/05/02 12:12:04 andrey Exp $ */
#ifndef PHP_MATH_H
#define PHP_MATH_H
@@ -60,6 +60,7 @@
PHP_FUNCTION(number_format);
PHP_FUNCTION(fmod);
PHP_FUNCTION(math_std_dev);
+PHP_FUNCTION(math_variance);
PHP_FUNCTION(deg2rad);
PHP_FUNCTION(rad2deg);
http://cvs.php.net/diff.php/php-src/ext/standard/tests/math/math_std_dev.phpt?r1=1.2&r2=1.3&ty=u
Index: php-src/ext/standard/tests/math/math_std_dev.phpt
diff -u php-src/ext/standard/tests/math/math_std_dev.phpt:1.2
php-src/ext/standard/tests/math/math_std_dev.phpt:1.3
--- php-src/ext/standard/tests/math/math_std_dev.phpt:1.2 Mon May 2 07:01:14 2005
+++ php-src/ext/standard/tests/math/math_std_dev.phpt Mon May 2 08:12:04 2005
@@ -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