Average of two numbers in php

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.

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?

asked Jan 10, 2010 at 2:13

array_sum[$values] / count[$values]

answered Nov 16, 2010 at 19:32

IsraIsra

6811 gold badge5 silver badges2 bronze badges

3

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];

Igor F.

2,5702 gold badges30 silver badges39 bronze badges

answered Jan 10, 2010 at 2:17

slebetmanslebetman

105k18 gold badges128 silver badges160 bronze badges

4

Average = Sum / Number of values

Just store all 3 values, there's no need for anything complicated.

If you store the Average and Sum then calculate Number of values you'll lose a little accuracy due to truncation of Average.

If you store the Average and Number of values then calculate Sum you'll lose even more accuracy. You have more margin for error in calculating a correct value for Number of values than Sum thanks to it being an integer.

answered Jan 10, 2010 at 2:20

MikeMike

1413 bronze badges

1

Thought that I should share my function

function avg[array $values] {
    $sum = array_sum[$values];
    $count = count[$values];
    return [$count !== 0]? $sum / $count: NAN;
}
echo avg[[1, 2, 3, 4]]; // 2.5

Will return the average and also take into account 0, for example dividing by zero always returns NaN [Not a number]

1/0 = NaN
0/0 = NaN

answered Aug 2, 2013 at 14:22

Timo HuovinenTimo Huovinen

51k33 gold badges144 silver badges136 bronze badges

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.

$oldsum = $average * $amount;
$newaverage = [$oldsum + $newnum] / [$amount + 1];

answered Jan 10, 2010 at 2:19

johannesjohannes

15.6k3 gold badges42 silver badges57 bronze badges

2

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.

answered Jan 10, 2010 at 2:20

Greg HewgillGreg Hewgill

909k177 gold badges1131 silver badges1267 bronze badges

This is called a 'running average' or 'moving average'.

If the database stores the average and the number of values averaged, it will be possible to calculate a new running average for each new value.

answered Jan 10, 2010 at 2:21

paviumpavium

14.4k4 gold badges31 silver badges48 bronze badges

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] 

How do you calculate standard deviation in PHP?

To calculate the standard deviation, we have to first calculate the variance. The variance can be calculated as the sum of squares of differences between all numbers and means. Finally to get the standard deviation we will use the formula, √[variance/no_of_elements].

Chủ Đề