简体   繁体   English

增加,减少百分比

[英]Increment, decrement by percentage

I'll try to explain this problem the best way i can with 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 what i need is a scaleable solution that gets back to the original value using only the count. 所以我需要的是一个可扩展的解决方案,只使用计数就可以恢复原始值。 Any ideas? 有任何想法吗?

The best way to do this is using a function. 最好的方法是使用一个函数。 It doesn't have to 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 back to 0 when they run out. 只需将numPowerUps更改为0即可。 This way, it looks a whole lot neater. 这样,它看起来整整很多。


An aside: 旁边:
The reason it's not working is because of the fact that adding and then subtracting percentages doesn't work. 它不起作用的原因是因为添加然后减去百分比不起作用。 For instance: 例如:

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'll always end up with 99% of your original value. 您总是会得到99%的原始价值。 If you really want to take a shortcut, you could instead 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're potentially susceptible to floating point errors. 但是,您可能容易受到浮点错误的影响。 The function way of doing it is not only neater and clearer, but potentially less error-prone. 这样做的功能方式不仅更整洁,更清晰,而且可能更不容易出错。

To reverse a %age increase, you must divide by the original %age, not subtract . 要反转%年龄增长,您必须除以原始的%年龄,而不是减去

ie: 即:

100 + 5% = 100 * 1.05 = 105

to reverse it: 扭转它:

105 / 1.05 = 100

The more usual '5% off' formula would instead give you: 更通常的“5%折扣”公式会改为:

105 - 5% = (105 * 0.95) = 99.75

To power up: 加电:

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

To power back down: 要退缩:

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

Let's take a more complicated example, 17 power ups, each giving 3% to an intial 1234 power: 让我们举一个更复杂的例子,17个电源UPS,每个电源提供3%的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 it out like that. 当你这样写出来时,它看起来很简单。

This doesn't work because the two percentages are not taken from the same number. 这不起作用,因为这两个百分比不是来自相同的数字。 They're 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. 第一次, power * 0.10为500,而5 * 500 = 2500,因此功率为5000 + 2500 = 7500。 Now, the power is 7500, so power * 0.10 is 750. 5*750 = 3750 and 7500-3750=3750 and not 5000 like you started out with. 现在,功率是7500,所以power * 0.10是750. 5 * 750 = 3750和7500-3750 = 3750而不是像你开始时的5000。

So apparently, what you want is not really to in/decrease by a percentage of the current power. 显然,你想要的并不是真正地减少当前功率的百分比。 Perhaps it would be better to set a base power (let's say 5000) and an actual power. 或许最好设置一个基本功率(假设为5000)和实际功率。 Then when you in/decrease, you use actualPower = actualPower + 5*0.1*basePower; 然后当你进/减时,你使用actualPower = actualPower + 5*0.1*basePower; or something. 或者其他的东西。 Or you just accept that five power downs after five power ups does not get you back to initial hp. 或者你只是接受五次加电后的五次断电并没有让你回到初始的HP。

I'm going to suspect that what you mean by "doesn't work" is that the value for power does not end up to be exactly 3750 . 我怀疑你所说的“不起作用”的意思是power的价值最终不会达到3750

This is due to floating-point rounding errors , as floating point values such as double and float are not able to be represented exact values. 这是由于浮点舍入错误 ,因为浮点值(如doublefloat无法表示精确值。

If exact values are needed, then using decimal or int would be a better solution, as they are designed to handle exact values. 如果需要精确值,那么使用decimalint将是更好的解决方案,因为它们旨在处理精确值。

Edit The actual issue here is not a floating-point rounding error, but an issue noted in Smashery's answer . 编辑这里的实际问题不是浮点舍入错误,而是Smashery的回答提到的一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM