Jump to content

Archived

This topic is now archived and is closed to further replies.

number9dream

Simple PHP maths question - updating an average

Recommended Posts

Hi,

I'm having some trouble with a calculation with PHP. It would be really helpful if someone could check my code for me!

It isn't anything too difficult: I just need to update an average number. I know the new number, the current average number and the total number of records. What I'm using is the below:

[code=php:0]
$new_average_number=((($average_number*$total_records)+$this number)/$total_records);
[/code]

Have I made any errors with this calculation, or should I be doing it a different way?

Thanks for any help :)

Share this post


Link to post
Share on other sites
Hi Redarrow,

Thanks for the reply.

Unless I've misunderstood, the mysql query doesn't really answer my question.

I'll try to explain what I'm trying to do:

It takes Mr Irrelevant an average of [code=php:0]$current_average[/code] minutes to eat an apple. The time taken has been recorded [code=php:0]$total_averages[/code] times. A new time has been recorded of [code=php:0]$new_average[/code]. What is the updated average ([code=php:0]$updated_average[/code])?


My answer:

[code=php:0]$updated_average=((($current_average*$total_averages)+$new_average)/$total_averages);[/code]

Share this post


Link to post
Share on other sites
Suppose you have an average of 8 based on 5 numbers.
You now have a new number of 20.

This should bring the average to ((5 * 8 ) + 20) / 6 which gives 10

with your code
[code]
$average_number = 8;
$total_records = 6;
$this_number = 20;

$new_average_number=((($average_number*$total_records)+$this_number)/$total_records);
echo $new_average_number; // --> 11.3333333
[/code]

So you need
[code]
$average_number = 8;
$total_records = 6;
$this_number = 20;

$new_average_number=((($average_number*($total_records-1))+$this_number)/$total_records);
echo $new_average_number; // --> 10
[/code]

You cannot calc the new average and the old total based on the same number count.

Share this post


Link to post
Share on other sites
I should have mentioned that I'm already updating the total number of averages:

[code=php:0]
$total_averages++;
$updated_average=((($current_average*$total_averages)+$new_average)/$total_averages);
[/code]

Share this post


Link to post
Share on other sites
I think it might be useful to explain how you are generating the original average and what your attempting to do by updating the average.

It looks like you have an total average, and how many seperate numbers were used to get that average. Then you are multiplying the average and count of numbers used to get the total number, add in the new average, and re-average it. If im correct in saying all that, then it sounds like a silly way of doing things. Explain what you are trying to do so we can give you the best way to do things.

Share this post


Link to post
Share on other sites
But you need the old total_averages for the first calculation:
[code]<?php
$ota = $total_averages;
$total_averages++;
$updated_average=((($current_average*$ota)+$new_average)/$total_averages);
?>[/code]

Ken

Share this post


Link to post
Share on other sites
[quote author=number9dream link=topic=99830.msg393346#msg393346 date=1152313842]
I should have mentioned that I'm already updating the total number of averages:

[code=php:0]
$total_averages++;
$updated_average=((($current_average*$total_averages)+$new_average)/$total_averages);
[/code]
[/quote]

But that doesn't correct the error that i pointed out to you

Share this post


Link to post
Share on other sites
No, you can see that in the first calculation Barand has used $total_averages-1

Share this post


Link to post
Share on other sites
I don't understand why I would take 1 away from the total averages. Can you explain why I need to do this?

Thanks again! :)

Added:

[quote]But you need the old total_averages for the first calculation:[/quote]

Ah, OK - I'm not calculating the total correctly, which is why a -1 would work.

Did I understand this, finally?

Share this post


Link to post
Share on other sites
Because your first average is based on x-1 numbers and your new is based on x. It's not rocket science.

Share this post


Link to post
Share on other sites
does this make sence anyone lol.............................


<?php

$current_average=10;

$total_averages=10;

$new_average=$current_average*$total_averages;

$current=$current_average;


$updated_average=$current_average*$total_averages+$new_average/$current;

echo $updated_average;

// 10*10 = 100 then + 100 = 200 then 10 divided by 10 = 110 current avareges
?>

Share this post


Link to post
Share on other sites
You can't be serious

if you have 10 * 10 then a new one of 100 the total is 200 and the new average is 200/11, which is
18.181818

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.