diff options
author | Andrey Hristov <andrey@php.net> | 2005-05-02 09:17:49 +0000 |
---|---|---|
committer | Andrey Hristov <andrey@php.net> | 2005-05-02 09:17:49 +0000 |
commit | fb0c19da1c0356327a111a15ecf2718877b36298 (patch) | |
tree | 0bd5b70aa601293fc09aaf61ee86355353838345 /ext/standard/math.c | |
parent | fe523b9a9b8ea3c7de165d496bd856e18132b03c (diff) | |
download | php-git-fb0c19da1c0356327a111a15ecf2718877b36298.tar.gz |
add math_std_dev()
Diffstat (limited to 'ext/standard/math.c')
-rw-r--r-- | ext/standard/math.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/ext/standard/math.c b/ext/standard/math.c index 289c3c491b..a1237165fd 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -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 |