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

From: Date: Sat, 07 May 2005 11:11:07 +0000
Subject: Re: cvs: php-src /ext/standard math.c /ext/standard/tests/math math_std_dev.phpt
References: 1  Groups: php.cvs 
Request: Send a blank email to php-cvs+get-31765@lists.php.net to get a copy of this message
You forgot to update the protos to add the second parameter. Nuno ----- Original Message ----- From: "Andrey Hristov" <andrey@php.net> To: <php-cvs@lists.php.net> Sent: Monday, May 02, 2005 1:29 PM Subject: [PHP-CVS] cvs: php-src /ext/standard math.c /ext/standard/tests/math math_std_dev.phpt
andrey Mon May 2 08:29:38 2005 EDT Modified files: /php-src/ext/standard math.c /php-src/ext/standard/tests/math math_std_dev.phpt Log: add possibility to calculate the standard deviation and the variance on a sample - the formulae are slightly different. http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.122&r2=1.123&ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.122 php-src/ext/standard/math.c:1.123 --- php-src/ext/standard/math.c:1.122 Mon May 2 08:12:04 2005 +++ php-src/ext/standard/math.c Mon May 2 08:29:37 2005 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: math.c,v 1.122 2005/05/02 12:12:04 andrey Exp $ */ +/* $Id: math.c,v 1.123 2005/05/02 12:29:37 andrey Exp $ */ #include "php.h" #include "php_math.h" @@ -1183,11 +1183,14 @@ /* }}} */ -static long double php_population_variance(zval *arr) +static long double php_population_variance(zval *arr, zend_bool sample) { double mean, sum = 0.0, vr = 0.0; zval **entry; HashPosition pos; + int elements_num; + + elements_num = 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) { @@ -1195,7 +1198,7 @@ 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)); + mean = sum / elements_num; 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) { @@ -1205,7 +1208,10 @@ vr += d*d; zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos); } - return (vr / zend_hash_num_elements(Z_ARRVAL_P(arr))); + if (sample) { + --elements_num; + } + return (vr / elements_num); } /* {{{ proto float math_variance(array a) @@ -1213,15 +1219,16 @@ PHP_FUNCTION(math_variance) { zval *arr; + zend_bool sample = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &arr, &sample) == 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(php_population_variance(arr, sample)); } /* }}} */ @@ -1231,15 +1238,16 @@ PHP_FUNCTION(math_std_dev) { zval *arr; + zend_bool sample = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &arr, &sample) == 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))); + RETURN_DOUBLE(sqrt(php_population_variance(arr, sample))); } /* }}} */


« previous php.cvs (#31765) next »