From 2011936fde3a198534d08214f671d562c0b06fb2 Mon Sep 17 00:00:00 2001 From: Morten Kolstad Date: Thu, 17 Oct 2024 10:56:27 +0200 Subject: [PATCH] Fix watt-to-ftp-calculation rounding issues When inputing watts in the editor, the resulting watts in the workout can differ by 1, because of flooring/rounding issues. This is fixed by rounding the wattFromFtpPct-calculation instead of flooring --- apps/frontend/src/utils/general.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/apps/frontend/src/utils/general.ts b/apps/frontend/src/utils/general.ts index cb034a89..5323cada 100644 --- a/apps/frontend/src/utils/general.ts +++ b/apps/frontend/src/utils/general.ts @@ -48,7 +48,24 @@ export const ftpPercentFromWatt = (watt: number, ftp: number) => Math.floor(1000 * (watt / ftp)) / 10; export const wattFromFtpPercent = (ftpPercent: number, ftp: number) => - Math.floor((ftpPercent * ftp) / 100); + Math.round((ftpPercent * ftp) / 100); + +let failures = []; +for (let ftp = 1; ftp <= 500; ftp += 1) { + for (let watt = 0; watt <= 1000; watt += 1) { + const ftpPct = Math.floor(1000 * (watt / ftp)) / 10; + const wattFromFtpPct = Math.round((ftpPct * ftp) / 100); + if (wattFromFtpPct !== watt) { + failures.push({ + watt, + exactWattFromFtpPct: (ftpPct * ftp) / 100, + ftpPct, + ftp, + }); + } + } +} +console.log('watt', failures); export const editable = (data: T): Editable => ({ touched: false, data }); export const touched = (data: T): Editable => ({ touched: true, data });