fix: rounding issues causing daily leaderboard to be out of order sometimes (@fehmer) (#6303)

This commit is contained in:
Christian Fehmer 2025-02-24 12:04:33 +01:00 committed by GitHub
parent 73182d450f
commit 7be66e9cb3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 2 deletions

View file

@ -62,6 +62,20 @@ describe("Misc Utils", () => {
timestamp: 1653591901000,
expectedScore: 1196200960717699,
},
{
wpm: 196.205,
acc: 96.075,
timestamp: 1653591901000,
expectedScore: 1196210960817699,
},
{
// this one is particularly important - in JS 154.39 * 100 is equal to 15438.999999999998
// thanks floating point errors!
wpm: 154.39,
acc: 96.14,
timestamp: 1740333827000,
expectedScore: 1154390961421373,
},
];
_.each(testCases, ({ wpm, acc, timestamp, expectedScore }) => {

View file

@ -67,8 +67,10 @@ export function matchesAPattern(text: string, pattern: string): boolean {
}
export function kogascore(wpm: number, acc: number, timestamp: number): number {
const normalizedWpm = Math.floor(wpm * 100);
const normalizedAcc = Math.floor(acc * 100);
// its safe to round after multiplying by 100 (99.99 * 100 rounded will be 9999 not 100)
// rounding is necessary to protect against floating point errors
const normalizedWpm = Math.round(wpm * 100);
const normalizedAcc = Math.round(acc * 100);
const padAmount = 100000;
const firstPart = (padAmount + normalizedWpm) * padAmount;