From deea37ba21bf4176a68148be7aa85c757f18ff7a Mon Sep 17 00:00:00 2001 From: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> Date: Fri, 3 Jul 2026 11:25:01 +0300 Subject: [PATCH] multiplying same-dimension quantities keeps a sane unit "2 kg * 500 g" multiplied the raw numbers (1000) and slapped the left unit on top, giving "1000 kg" instead of "1 kg". Convert the right operand into the left unit first for same-dimension products; cross-dimension multiplication ($12 * 3, 5 hours * 2) is unaffected. --- src/engine/edge.test.ts | 5 +++++ src/engine/evaluator.ts | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/engine/edge.test.ts b/src/engine/edge.test.ts index 8f5517b..6c82046 100644 --- a/src/engine/edge.test.ts +++ b/src/engine/edge.test.ts @@ -219,4 +219,9 @@ describe("review fixes", () => { it("random() participates in arithmetic", () => { expect(calc("random() + 1")).not.toBeNull(); }); + it("multiplying same-dimension quantities converts the right operand", () => { + expect(calc("2 kg * 500 g")).toBe("1 kg"); + expect(calc("$12 * 3")).toBe("$36"); + expect(calc("2 m * 3 m")).toBe("6 m²"); + }); }); diff --git a/src/engine/evaluator.ts b/src/engine/evaluator.ts index 2dc95ca..c34631d 100644 --- a/src/engine/evaluator.ts +++ b/src/engine/evaluator.ts @@ -252,6 +252,8 @@ function evalBin(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value, const u = ctx.reg.unitsById.get(targetId); if (u) return qty(baseProduct.div(u.ratio), u); } + // same dimension: "2 kg * 500 g" scales in the left unit, not 2*500 + if (da === db) return qty(a.value.mul(convertQ(b, a.unit).value), a.unit); return qty(a.value.mul(b.value), a.unit); } return qty(a.value.mul(b.value), a.unit ?? b.unit ?? null);