略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: stats_harmonic_mean

2024-12-26

stats_harmonic_mean

(PECL stats >= 1.0.0)

stats_harmonic_meanReturns the harmonic mean of an array of values

说明

stats_harmonic_mean ( array $a ) : number

Returns the harmonic mean of the values in a.

参数

a

The input array

返回值

Returns the harmonic mean of the values in a, or FALSE if a is empty or is not an array.

add a note add a note

User Contributed Notes 1 note

up
4
Daniel Morris (www.danielxmorris.com)
10 years ago
I wrote this to calculate harmonic averages in my shares program.  Hopefully this can help someone who doesn't want to use PECL.  The function accepts an unlimited number of arguments without the need to place them into an array, which is better than the way the PECL extension handles the function.

<?php

/**
*  @author    Daniel Morris
*/
function harmonic () {
   
$num_args = func_num_args ();
    for (
$i = 0; $i < $num_args; $i++) {
       
$sum += 1 / func_get_arg ($i);
    }
    return
$num_args / $sum;
}

// Tests

echo harmonic (1, 2, 3, 4, 5);
echo
harmonic (-1, 3, 4, 10293);
echo
harmonic (-1, -2, -3, -4, -5);

//  2.1897810218978
//  -9.6022389365052
//  -2.1897810218978 

?>

官方地址:https://www.php.net/manual/en/function.stats-harmonic-mean.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3