Jump to content


Photo

Simple PHP maths question - updating an average


  • Please log in to reply
15 replies to this topic

#1 number9dream

number9dream
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 July 2006 - 10:21 PM

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:

$new_average_number=((($average_number*$total_records)+$this number)/$total_records);

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

Thanks for any help :)

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 07 July 2006 - 10:25 PM

SELECT AVG(Sales) FROM Store_Information
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 number9dream

number9dream
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 July 2006 - 10:59 PM

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
$current_average
minutes to eat an apple. The time taken has been recorded
$total_averages
times. A new time has been recorded of
$new_average
. What is the updated average (
$updated_average
)?


My answer:

$updated_average=((($current_average*$total_averages)+$new_average)/$total_averages);



#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 July 2006 - 11:04 PM

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
$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

So you need
$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

You cannot calc the new average and the old total based on the same number count.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 number9dream

number9dream
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 July 2006 - 11:10 PM

I should have mentioned that I'm already updating the total number of averages:

$total_averages++;
$updated_average=((($current_average*$total_averages)+$new_average)/$total_averages);


#6 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 07 July 2006 - 11:20 PM

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.

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 July 2006 - 11:21 PM

But you need the old total_averages for the first calculation:
<?php
$ota = $total_averages;
$total_averages++;
$updated_average=((($current_average*$ota)+$new_average)/$total_averages);
?>

Ken


#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 July 2006 - 11:24 PM

I should have mentioned that I'm already updating the total number of averages:

$total_averages++;
$updated_average=((($current_average*$total_averages)+$new_average)/$total_averages);


But that doesn't correct the error that i pointed out to you
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 07 July 2006 - 11:24 PM

No, you can see that in the first calculation Barand has used $total_averages-1

#10 number9dream

number9dream
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 July 2006 - 11:28 PM

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:

But you need the old total_averages for the first calculation:


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

Did I understand this, finally?

#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 July 2006 - 11:33 PM

Because your first average is based on x-1 numbers and your new is based on x. It's not rocket science.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#12 number9dream

number9dream
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 July 2006 - 11:35 PM

It's not rocket science.


OK, OK. I did say it was a simple question! ;)

#13 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 07 July 2006 - 11:37 PM

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
?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#14 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 July 2006 - 11:47 PM

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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#15 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 07 July 2006 - 11:51 PM

i tried lol with the coding part sorry.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#16 number9dream

number9dream
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 08 July 2006 - 12:04 AM

Forgot to say thanks to Barand for explaining this to me! :)

(Don't bite the n00bies, though, eh ;))




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users