Jump to content

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 :)
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]

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.
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 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
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?
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
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.