Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

fix(plates): preserve fractional deficits and never exceed the target

AC674f795bdfd4
diff --git a/frontend/src/lib/plates.js b/frontend/src/lib/plates.js
index a3fe9f965..0fa748d55 100644
--- a/frontend/src/lib/plates.js
+++ b/frontend/src/lib/plates.js
@@ -111,8 +111,9 @@ const q = w => Math.round(w * Q)
* → { plates: [w, …] heaviest first, missing: number } (missing 0 = exact)
*/
export function plateStack(weight, inv) {
- const target = q(Math.max(0, num(weight)))
- if (target === 0) return { plates: [], missing: 0 }
+ const requested = Math.max(0, num(weight))
+ const target = Math.floor(requested * Q)
+ if (target === 0) return { plates: [], missing: requested }
const items = (inv || []).filter(p => p.w > 0 && p.n > 0).sort((a, b) => b.w - a.w)
// 1. greedy
const greedy = []
@@ -126,7 +127,7 @@ export function plateStack(weight, inv) {
// missing. The search below would only find that again, with a table as long as the target:
// a typo like 9999 in a weight field built it on every keystroke.
const all = items.reduce((sum, p) => sum + q(p.w) * p.n, 0)
- if (left === 0 || target >= all) return { plates: greedy, missing: left / Q }
+ if (left === 0 || target >= all) return { plates: greedy, missing: requested - (target - left) / Q }
// 2. exact fit with the fewest plates: f[i][s] = min plates using sizes i.. to make s
const n = items.length
const INF = 1e9
@@ -159,7 +160,7 @@ export function plateStack(weight, inv) {
}
}
}
- return { plates, missing: (target - sum) / Q }
+ return { plates, missing: requested - sum / Q }
}
/**
diff --git a/frontend/src/lib/plates.test.js b/frontend/src/lib/plates.test.js
index 0e6708ef3..20ba7ed50 100644
--- a/frontend/src/lib/plates.test.js
+++ b/frontend/src/lib/plates.test.js
@@ -235,3 +235,18 @@ describe('dropGrid', () => {
expect(dropGrid(S, { id: db, inc: 2 })).toBe(2)
})
})
+
+describe('loads between quarter-unit boundaries', () => {
+ test.each([0.1, 2.4, 2.6, 7.4, 8.1])('never exceeds %s and reports the actual deficit', weight => {
+ const result = plateStack(weight, [{ w: 2.5, n: 3 }])
+ const loaded = result.plates.reduce((sum, w) => sum + w, 0)
+ expect(loaded).toBeLessThanOrEqual(weight)
+ expect(result.missing).toBeCloseTo(weight - loaded)
+ })
+
+ test('drop sets snap to a load the counted plates can make', () => {
+ const S = { unit: 'kg', plates: { kg: { 5: 3 } } }
+ const snap = dropGrid(S, { id: idOf('barbell') })
+ expect(snap(48.74)).toBe(40)
+ })
+})