PHP的数学平均值 [英] Math average with php
问题描述
该测试您的数学技能了……
Time to test your math skills...
我正在使用php查找$ num1,$ num2,$ num3等的平均值;直至未设置数量的数字.然后将该平均值保存到数据库中.
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脚本称为新数字时,将其添加到组合中.
Next time the php script is called a new number is added to the mix.
是否存在一个数学(最可能是代数)方程,可以用来查找包含新数字的原始数字的平均值.还是我需要将原始数字保存在数据库中以便查询并重新计算整个数字?
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?
推荐答案
如果您的平均意思是平均值",并且您不希望存储所有数字,则存储其数量:
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);
这篇关于PHP的数学平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!