mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-11 08:05:56 +08:00
fix: rounding issues causing daily leaderboard to be out of order sometimes (@fehmer) (#6303)
This commit is contained in:
parent
73182d450f
commit
7be66e9cb3
2 changed files with 18 additions and 2 deletions
|
@ -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 }) => {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue