用 php 计算平均数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2035798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Math average with php
提问by Marcus
Time to test your math skills...
是时候测试你的数学技能了……
I'm using php to find the average of $num1, $num2, $num3 and so on; upto an unset amount of numbers. It then saves that average to a database.
我正在使用 php 查找 $num1、$num2、$num3 等的平均值;最多未设置数量的数字。然后将该平均值保存到数据库中。
Next time the php script is called a new number is added to the mix.
下次调用 php 脚本时,将添加一个新数字。
Is there a math (most likely algebra) equation that I can use to find the average of the original numbers with the new number included. Or do I need to save the original numbers in the database so I can query them and re-calculate the entire bunch of numbers together?
是否有一个数学(很可能是代数)方程,我可以用它来找到包含新数字的原始数字的平均值。或者我是否需要将原始数字保存在数据库中,以便我可以查询它们并重新计算整组数字?
采纳答案by slebetman
If what you mean by average is the 'mean' and you don't want to store all numbers then store their count:
如果您所说的平均值是“平均值”并且您不想存储所有数字,则存储它们的计数:
$last_average = 100;
$total_numbers = 10;
$new_number = 54;
$new_average = (($last_average * $total_numbers) + $new_number) / ($total_numbers + 1);
回答by Isra
array_sum($values) / count($values)
回答by Mike
Average = Sum / Number of values
Just store all 3 values, there's no need for anything complicated.
只需存储所有 3 个值,无需任何复杂操作。
If you store the Averageand Sumthen calculate Number of valuesyou'll lose a little accuracy due to truncation of Average.
如果您存储Average和Sum再计算Number of values你会失去一点点的准确性,由于平均的截断。
If you store the Averageand Number of valuesthen calculate Sumyou'll lose even more accuracy. You have more margin for error in calculating a correct value for Number of valuesthan Sumthanks to it being an integer.
如果您存储Average和Number of values然后计算Sum你就会失去更多的精度。您有误差计算正确值更多余地Number of values不是Sum由于它是一个整数。
回答by Hamidreza
<?php
function avrg()
{
$count = func_num_args();
$args = func_get_args();
return (array_sum($args) / $count);
}
?>
回答by Timo Huovinen
Thought that I should share my function
认为我应该分享我的功能
function avg($sum=0,$count=0){
return ($count)? $sum / $count: NAN;
}
var_dump( avg(array_sum($values),count($values)) );
Will return the average and also take into account 0, for example dividing by zero always returns NaN (Not a number)
将返回平均值并考虑 0,例如除以零总是返回 NaN(不是数字)
1/0 = NaN
0/0 = NaN
1/0 = NaN
0/0 = NaN
回答by Greg Hewgill
Typically what you might do is save two pieces of information:
通常,您可能会保存两条信息:
- the sum of all the numbers
- the count of numbers
- 所有数字的总和
- 数字的计数
Whenever you want to get the average, divide the sum by the count (taking care for the case of count == 0, of course). Whenever you want to include a new number, add the new number to the sum and increment the count by 1.
每当您想获得平均值时,将总和除以计数(当然要注意 count == 0 的情况)。每当您想包括一个新数字时,将新数字添加到总和并将计数增加 1。
回答by johannes
If you know the amount of numbers you can calculate the old sum, add the new one and divide by the old amount plus one.
如果你知道数字的数量,你可以计算旧的总和,加上新的数字,然后除以旧的数字加 1。
$oldsum = $average * $amount;
$newaverage = ($oldsum + $newnum) / ($amount + 1);
回答by pavium
This is called a 'running average' or 'moving average'.
这称为“运行平均”或“移动平均”。
If the database stores the average andthe number of values averaged, it will be possible to calculate a new running average for each new value.
如果数据库存储了平均值和平均值的数量,则可以为每个新值计算新的运行平均值。
回答by Harold
function avgvals($avg_vals,$avg_delimiter=',') {
if ( (is_string($avg_vals) && strlen($avg_vals) > 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) {
$average_vals = explode($avg_delimiter, $avg_vals);
$return_vals = ( array_sum($average_vals) / count($average_vals) );
} elseif ( (is_string($avg_vals) && strlen($avg_vals) <= 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) {
$return_vals = $avg_vals;
} else {
$return_vals = FALSE;
}
return $return_vals;
}
回答by Alix Axel
You need to save all the original numbers in the database.
您需要将所有原始数字保存在数据库中。

