BUGFIX: Fix calculateExp so that it won't return a too small result (#2274)

* BUGFIX: Fix calculateExp so that it won't return a too small result

Due to floating point rounding issues, when applying the inverse
operation and flooring, roughly half the time we would get the next
lower skill level. This quickly finds a slightly higher result that
gives the correct inverse.

* clearer test layout
This commit is contained in:
David Walker
2025-07-31 12:54:31 -07:00
committed by GitHub
parent 8976d54532
commit d4cb50fbf1
2 changed files with 49 additions and 1 deletions
+15 -1
View File
@@ -10,7 +10,21 @@ export function calculateSkill(exp: number, mult = 1): number {
}
export function calculateExp(skill: number, mult = 1): number {
const value = Math.exp((skill / mult + 200) / 32) - 534.6;
const floorSkill = Math.floor(skill);
let value = Math.exp((skill / mult + 200) / 32) - 534.6;
if (skill === floorSkill && Number.isFinite(skill)) {
// Check for floating point rounding issues that would cause the inverse
// operation to return the wrong result.
let calcSkill = calculateSkill(value, mult);
let diff = Math.abs(value * Number.EPSILON);
let newValue = value;
while (calcSkill < skill) {
newValue = value + diff;
diff *= 2;
calcSkill = calculateSkill(newValue, mult);
}
value = newValue;
}
return clampNumber(value, 0);
}