Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix watt-to-ftp-calculation rounding issues #414

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

morteako
Copy link
Collaborator

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

Example
FTP: 280, input 279
ftpPct = Math.floor(1000 * (279 / 280)) / 10;
ftPct = 99.6

result in editor: 279, 99.6%

when we then calculate in the workout:
Math.floor((ftpPct * ftp) / 100) = Math.floor((27888 / 100) = 278;

Based on my tests, changing the floor in the wattFromFtpPct to round, should fix this.
Since

Gif of error in action : 279 => 278

2024-10-22 20 51 32

Testing

Testing code old version

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.floor((ftpPct * ftp) / 100);
    if (wattFromFtpPct !== watt) {
      failures.push({
        watt,
        exactWattFromFtpPct: (ftpPct * ftp) / 100,
        ftpPct,
        ftp,
      });
    }
  }
}
console.log(failures);

=> Lots of issues
image

Testing code for new version

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(failures);
image

=> All watts are correct

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant