number9dream Posted July 7, 2006 Share Posted July 7, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/ Share on other sites More sharing options...
redarrow Posted July 7, 2006 Share Posted July 7, 2006 SELECT AVG(Sales) FROM Store_Information Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54576 Share on other sites More sharing options...
number9dream Posted July 7, 2006 Author Share Posted July 7, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54592 Share on other sites More sharing options...
Barand Posted July 7, 2006 Share Posted July 7, 2006 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 10with 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. Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54594 Share on other sites More sharing options...
number9dream Posted July 7, 2006 Author Share Posted July 7, 2006 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 Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54597 Share on other sites More sharing options...
Crimpage Posted July 7, 2006 Share Posted July 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54600 Share on other sites More sharing options...
kenrbnsn Posted July 7, 2006 Share Posted July 7, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54601 Share on other sites More sharing options...
Barand Posted July 7, 2006 Share Posted July 7, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54602 Share on other sites More sharing options...
Crimpage Posted July 7, 2006 Share Posted July 7, 2006 No, you can see that in the first calculation Barand has used $total_averages-1 Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54603 Share on other sites More sharing options...
number9dream Posted July 7, 2006 Author Share Posted July 7, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54605 Share on other sites More sharing options...
Barand Posted July 7, 2006 Share Posted July 7, 2006 Because your first average is based on x-1 numbers and your new is based on x. It's not rocket science. Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54607 Share on other sites More sharing options...
number9dream Posted July 7, 2006 Author Share Posted July 7, 2006 [quote]It's not rocket science.[/quote]OK, OK. I did say it was a simple question! ;) Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54608 Share on other sites More sharing options...
redarrow Posted July 7, 2006 Share Posted July 7, 2006 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?> Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54611 Share on other sites More sharing options...
Barand Posted July 7, 2006 Share Posted July 7, 2006 You can't be seriousif you have 10 * 10 then a new one of 100 the total is 200 and the new average is 200/11, which is18.181818 Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54614 Share on other sites More sharing options...
redarrow Posted July 7, 2006 Share Posted July 7, 2006 i tried lol with the coding part sorry. Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54618 Share on other sites More sharing options...
number9dream Posted July 8, 2006 Author Share Posted July 8, 2006 Forgot to say thanks to Barand for explaining this to me! :)(Don't bite the n00bies, though, eh ;)) Quote Link to comment https://forums.phpfreaks.com/topic/13987-simple-php-maths-question-updating-an-average/#findComment-54621 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.