The XP Bar Is Full, but the Level Will Not Unlock
When the bar is full and the level does not come, the experience is almost always a hair short: decimals added up in a computer land just below the whole number. 0.1 + 64.1 + 35.8 is 100 on paper and 99.99999999999999 in the computer, and rounding down makes that 99. The same hair made a sale in our own blueprints pay one coin too few. Z3 found the first of these by adding the numbers up the way a game does.
What you see
- The experience bar looks full, it may even say 100 / 100, and the next level or tier stays locked until a little more experience comes in.
- It happens after rewards with decimals: bonuses such as times 1.1, shared experience, or rewards for part of a task.
- A shop pays one coin less than the price times the share: selling for 100 at a share of 0.29 pays 28 instead of 29, and earning 100 with a bonus of 1.15 pays 114 instead of 115.
- With whole numbers only, no shares and no bonuses, it never happens, which makes it look random.
Why is the bar a hair short?
A computer keeps decimals in binary, and most decimals, 0.1 among them, have no exact binary form. Each one is stored a tiny bit off, and adding them up can leave the sum a tiny bit below the whole number. As long as you only show the number, nobody sees it. But our season track worked out the tier reached with a plain round-down of experience divided by experience per tier, so 99.99999999999999 of 100 was tier 0. Z3 counts in the same 64-bit numbers a game uses, and its check of the blueprint found that 0.1 + 64.1 + 35.8 falls short of 100.
Our currency wallet did the same with a product: 100 × 0.29 is 28.999999999999996 in the computer, which rounds down to 28. That one was fixed in the same round of checks; our notes do not say which tool found it.
How to check your game for it
Use numbers you know land a hair short, and ask for the whole result. The first test adds up 0.1, 64.1 and 35.8 on tiers of 100 and asks for tier 1. The second sells for 100 at a share of 0.29 and earns 100 with a bonus of 1.15. The third makes sure the fix does not round up what is really short: 99.99 of 100 is still not a tier.
The test: fix.test.ts
import assert from "node:assert/strict";
import test from "node:test";
import { coinsFor, tierReached } from "./fix.ts";
test("experience that adds up to a full tier unlocks it", () => {
let xp = 0;
for (const award of [0.1, 64.1, 35.8]) xp += award;
assert.equal(tierReached(xp, 100), 1, `${xp} experience on tiers of 100`);
});
test("selling and bonuses pay the coins the price says", () => {
assert.equal(coinsFor(100, 0.29), 29, "selling for 100 at a share of 0.29");
assert.equal(coinsFor(100, 1.15), 115, "earning 100 with a bonus of 1.15");
});
test("a hair below is still below when it is more than rounding", () => {
assert.equal(tierReached(99.99, 100), 0);
assert.equal(tierReached(250, 100), 2);
assert.equal(coinsFor(7, 0.5), 3);
});
The catch:
- experience that adds up to a full tier unlocks it
- selling and bonuses pay the coins the price says
What it said on the code with the bug put back
✖ experience that adds up to a full tier unlocks it
99.99999999999999 experience on tiers of 100
got 0, expected 1
✖ selling and bonuses pay the coins the price says
selling for 100 at a share of 0.29
got 28, expected 29
✔ a hair below is still below when it is more than rounding
3 tests: 1 passed, 2 failedWhat it said on the fixed code
✔ experience that adds up to a full tier unlocks it
✔ selling and bonuses pay the coins the price says
✔ a hair below is still below when it is more than rounding
3 tests: 3 passed, 0 failedThe fix
Add a tiny allowance before you round down: a value within 0.000000001 of the next whole number counts as that number. That is far smaller than any reward a game gives, and far larger than the error adding decimals leaves behind. Our season track and wallet both do it this way now.
The cleaner way is to keep such numbers whole: count experience in whole points and coins in whole coins, and turn a bonus into whole points once, when it is given. Then there is nothing to round.
What changed
/** The tier (or level) reached with this much experience. */
export function tierReached(xp: number, xpPerTier: number): number {
- return Math.floor(xp / xpPerTier);
+ return Math.floor(xp / xpPerTier + DUST);
}
/** Whole coins for a whole price times a share (selling back, or a bonus), rounded down. */
export function coinsFor(price: number, share: number): number {
- return Math.floor(price * share);
+ return Math.floor(price * share + DUST);
}The fixed code: fix.ts
/* Numbers that should come out whole but land a hair below it. A computer adds and multiplies
decimals in binary, so 0.1 + 64.1 + 35.8 is 99.99999999999999, not 100. Rounding down then
misses by one. From two blueprints:
https://spielerando.com/en/mechanics/season-track
https://spielerando.com/en/mechanics/currency-wallet */
/** What adding up decimals can leave behind: a value this close counts as reached. */
const DUST = 1e-9;
/** The tier (or level) reached with this much experience. */
export function tierReached(xp: number, xpPerTier: number): number {
return Math.floor(xp / xpPerTier + DUST);
}
/** Whole coins for a whole price times a share (selling back, or a bonus), rounded down. */
export function coinsFor(price: number, share: number): number {
return Math.floor(price * share + DUST);
}
Paste this to your AI
In my game a bar can be full and the next level (or tier) still stays locked, or a sale pays one coin less than it should. I think decimals added up in the computer land a hair below the whole number, and rounding down then misses by one.
1. Before you change anything, list every place where my game rounds down, cuts off decimals, or compares against a whole number to decide a level, a tier, an unlock or a payout. For each, write down which decimals can flow into it (rewards, bonuses, shares, multipliers).
2. Write tests like the ones below for my code: rewards of 0.1, 64.1 and 35.8 on a level of 100 must reach the level, and selling for 100 at a share of 0.29 must pay 29. Run them and paste me the real output.
3. Fix what fails the way the code below does: allow a tiny rounding error (0.000000001) before rounding down. Better still, keep experience and coins in whole numbers. Run the tests again and paste the output.
4. Take the tiny allowance out again for a moment, show me which tests fail, and put it back.
Do not tell me it is fixed unless the test output shows it.
The code, from https://spielerando.com/en/fixes/full-bar-but-no-level-up (TypeScript, tested):
```ts
/* Numbers that should come out whole but land a hair below it. A computer adds and multiplies
decimals in binary, so 0.1 + 64.1 + 35.8 is 99.99999999999999, not 100. Rounding down then
misses by one. From two blueprints:
https://spielerando.com/en/mechanics/season-track
https://spielerando.com/en/mechanics/currency-wallet */
/** What adding up decimals can leave behind: a value this close counts as reached. */
const DUST = 1e-9;
/** The tier (or level) reached with this much experience. */
export function tierReached(xp: number, xpPerTier: number): number {
return Math.floor(xp / xpPerTier + DUST);
}
/** Whole coins for a whole price times a share (selling back, or a bonus), rounded down. */
export function coinsFor(price: number, share: number): number {
return Math.floor(price * share + DUST);
}
```
The test that catches it:
```ts
import assert from "node:assert/strict";
import test from "node:test";
import { coinsFor, tierReached } from "./fix.ts";
test("experience that adds up to a full tier unlocks it", () => {
let xp = 0;
for (const award of [0.1, 64.1, 35.8]) xp += award;
assert.equal(tierReached(xp, 100), 1, `${xp} experience on tiers of 100`);
});
test("selling and bonuses pay the coins the price says", () => {
assert.equal(coinsFor(100, 0.29), 29, "selling for 100 at a share of 0.29");
assert.equal(coinsFor(100, 1.15), 115, "earning 100 with a bonus of 1.15");
});
test("a hair below is still below when it is more than rounding", () => {
assert.equal(tierReached(99.99, 100), 0);
assert.equal(tierReached(250, 100), 2);
assert.equal(coinsFor(7, 0.5), 3);
});
```
In my game a bar can be full and the next level (or tier) still stays locked, or a sale pays one coin less than it should. I think decimals added up in the computer land a hair below the whole number, and rounding down then misses by one.
1. Before you change anything, list every place where my game rounds down, cuts off decimals, or compares against a whole number to decide a level, a tier, an unlock or a payout. For each, write down which decimals can flow into it (rewards, bonuses, shares, multipliers).
2. Write tests like the ones below for my code: rewards of 0.1, 64.1 and 35.8 on a level of 100 must reach the level, and selling for 100 at a share of 0.29 must pay 29. Run them and paste me the real output.
3. Fix what fails the way the code below does: allow a tiny rounding error (0.000000001) before rounding down. Better still, keep experience and coins in whole numbers. Run the tests again and paste the output.
4. Take the tiny allowance out again for a moment, show me which tests fail, and put it back.
Do not tell me it is fixed unless the test output shows it.Where we found it
The blueprint the bug was in, and the test that guards it there:
- Season Track:
edge: awards with fractions that add up to a tier reach it - Currency Wallet:
edge-a-share-with-decimals-rounds-down-to-the-right-coin
How we found them: the bugs our proofs found in our own code
Was this page helpful?
Next step
Fix by symptom