mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-25 10:42:51 +02:00
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:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user