Skip to content

Commit

Permalink
fix: Fix watt-to-ftp-calculation rounding issues
Browse files Browse the repository at this point in the history
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
  • Loading branch information
morteako authored and sivertschou committed Jan 11, 2025
1 parent d4aa7da commit 3fc01ca
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion apps/frontend/src/utils/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T>(data: T): Editable<T> => ({ touched: false, data });
export const touched = <T>(data: T): Editable<T> => ({ touched: true, data });
Expand Down

0 comments on commit 3fc01ca

Please sign in to comment.