2
I’m having trouble making a simple monthly growth calculation using PHP. There is the problem that it is not possible to divide by zero.
The formula used is as follows::
$taxaDeCrescimento = ((($quantidadeMesATUAL - $quantidadeMesPassado) / $quantidadeMesPassado) * 100);
I’m basing this calculation on this formula of growing up.
But I would like this calculation to work in months past where it obtained the value of quantity equal to zero. That is, if last month got quantity 0 and this month had quantity 2. Get a growth percentage of 200%. What would be the solution to this problem?
What you’re calculating is relative growth and there’s no way to calculate that from zero. Having a 200% growth rate means that you have doubled the previous value. If it was 100, it was 200; if it was 500, it was 1000. The growth rate from 0 to 2 (or any value) tends to infinity.
– Woss
It’s a college exercise ???
– novic
No @Virgilionovic, it’s a problem I’m solving in a system
– Arthur Abitante
@Andersoncarloswoss this form in the practice of the system I’m doing would work perfectly. Because the "quantityMesAtual" never reaches zero. But when it comes to performing my tests if they are functional, I realize that every month with such a "quantity" there are several that reach zero. Because there was nothing inserted in the perido so that this amount is not zero.
– Arthur Abitante
I am mainly in doubt of what I can do about it.
– Arthur Abitante
But if the zero quantity situation should not happen in production, then I see no reason to consider it in the account. You just make a
if
checking if it is zero and returning an error.– Woss