Increase, decrease in percentage

I will try to explain this problem in the best way with the code:

double power = 5000; //picked up 5 power ups, now need to increase power by 10% per powerup power += 5 * (power * .10); //later on...ran into 5 power downs need to decrease power back to initial hp power -= 5 * (power * .10);//7500 - 3750 -- doesn't work 

So, I need a scalable solution that returns to its original value using only the count. Any ideas?

+6
math c #
source share
5 answers

The best way to do this is to use a function. It should not look exactly like this, but:

 class Whatever { private double basePower = 5000; public int numPowerUps = 5; public double GetActualPower() { return basePower + (numPowerUps * basePower * 0.1); } } 

Just change numPowerUps to 0 when done. Thus, he looks much neat.


To the side:
The reason it does not work is because adding and then subtracting percent does not work. For example:

 1. What is 10% of 100? --> 10 2. Add that to the 100 --> 110 3. What is 10% of 110? --> 11 4. Subtract that from 110 --> 99 

You will always be 99% of your original value. If you really want to use a shortcut, you can do this:

 1. What is 10% of 100? --> 10 2. Add that to the 100 --> 110 3. What is (100/11) = 9.09090909...% of 110? --> 10 4. Subtract that from 110 --> 100 

But then you are potentially prone to floating point errors. A functional way to do this is not only more accurate and understandable, but also potentially less error prone.

+19
source share

To reverse the% increase in age, you must divide by the original% age, not subtract.

i.e:.

 100 + 5% = 100 * 1.05 = 105 

to cancel it:

 105 / 1.05 = 100 

The more common β€œ5% off” formula will instead give you:

 105 - 5% = (105 * 0.95) = 99.75 
+4
source share

To turn on the power:

 power <- power * (1 + count * percent); eg: 5000 * (1 + 5 * 0.1) 5000 * 1.5 7500 

To turn off the power:

 power <- power / (1 + count * percent) eg: 7500 / (1 + 5 * 0.1) 7500 / 1.5 5000 

Let's take a more complex example: 17 bonuses, each of which gives 3% of the main power of 1234:

  1234 * (1 + 17 * 0.3) = 1234 * (1 + 5.1) = 1234 * 6.1 = 7527.4 7527.4 / (1 + 17 * 0.3) = 7527.4 / (1 + 5.1) = 7527.4 / 6.1 = 1234 

It actually looks pretty simple when you write this.

+4
source share

This does not work, because two percent are not taken from the same number. They are taken from the same variable, but not the same number.

The first time power * 0.10 is 500 and 5 * 500 = 2500, so the power will be 5000 + 2500 = 7500. Now the power is 7500, so power * 0.10 is 750. 5 * 750 = 3750 and 7500-3750 = 3750, not 5000, as you started with.

Thus, it is obvious that you want to not be / reduced by a percentage of the current capacity. Perhaps it would be better to set the base power (say 5000) and the actual power. Then, when you in / decrease, you use actualPower = actualPower + 5*0.1*basePower; or something like that. Or you just accept that five power outages after five bonuses do not return you to your original speed.

+2
source share

I'm going to suspect that what you mean by "doesn't work" is that the value for power does not have to be exactly 3750 .

This is due to rounding errors with floating point , floating point values such as double and float cannot be represented with exact values.

If exact values ​​are needed, then decimal or int will be the best solution, since they are designed to handle exact values.

Change The actual problem here is not a floating point rounding error, but the problem is noted in Smashery's answer .

+1
source share

All Articles