multiplying a float below 1.0 subtracts from number

I'm writing a function to give the player XP and level up, and I set the multiplier to 0.9 and it subtracts from the experienceNeeded instead of multiplying. I did the calculations on my calculator, in the game it just infinitely levels up. I'm not good with math so i dont know whats going on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Player::GiveExperience(int xpToGive)
{
	const float multiplier = 0.9; //Amount to multiply XP needed by.
	unsigned levelUpAmount = 0;

	experience += xpToGive;

	//This wiil apply all the levels the player earns if they get a huge XP boost that spans multiple levels.
	while (experience >= experienceNeeded)
	{
		levelUpAmount++;
		experienceNeeded = experienceNeeded * multiplier;
		cout << "Level up amount " << levelUpAmount << endl; //Debug only
	}
	LevelUp(levelUpAmount);
}


If your while loop is entered, this means that experience >= experienceNeeded.
The only (relevant) thing you do in the while loop is re-assign experienceNeeded. If you're multiplying by an amount less than 1.0, experienceNeeded could never become higher than experience.

Perhaps you meant experienceNeeded += experienceNeeded * multiplier;
Just a guess.
Or, ensure that your multiplier is greater than 1.0.

Also, if you're mixing ints and floats, be aware that the result of a floating-point number will be truncated if assigned back into an int.
Last edited on
I set the multiplier to 0.9 and it subtracts from the experienceNeeded instead of multiplying
Yes, if you multiply two numbers and one of the numbers is less than 1, the result will be less than the other number.

(Assuming x > 0)
x * 0.9 < x

That's how multiplication works. I don't know you were expecting, but if it was different, then it was wrong I'm sorry to say.


Honestly, I don't know what sort of answer you're hoping for. Yes, the code you have there will likely loop forever, but we can't fix it for you if we don't even know what sort of experience curve you had in mind. The best I can you is, give 'multiplier' a value greater than 1? Sorry, but that's the best I can do with this much information.
Topic archived. No new replies allowed.