. 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)
/* }}} */
-
-/* {{{ 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);
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)));
}
/* }}} */
--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