From 2e507b4882292de85f07d486d6863861161a3f19 Mon Sep 17 00:00:00 2001 From: carlacostea Date: Fri, 30 Oct 2026 09:55:06 +0200 Subject: [PATCH 1/4] fix(symbolic): stop treating large numbers as infinity (PIE-1418) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit normalize() replaced any constant result >= 1,497,258 (or <= -1,497,258) with Infinity, so 2000000 and 3000000 were scored as equal. The cutoff was tan(90°) under the 355/113 approximation of pi. Undefined trigonometric values are now detected directly: before normalizing, tan/sec at odd multiples of pi/2 and cot/csc at multiples of pi become Infinity, and sin/cos at their zeros become an exact 0. A constant expression containing an undefined value is evaluated numerically, since simplify treats Infinity as a variable. The size cutoff is removed, so large numbers are compared by value. --- src/symbolic/index.ts | 105 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 97 insertions(+), 8 deletions(-) diff --git a/src/symbolic/index.ts b/src/symbolic/index.ts index 1e14c72..a4d59ed 100644 --- a/src/symbolic/index.ts +++ b/src/symbolic/index.ts @@ -1,14 +1,19 @@ import { logger } from "../log"; import { mathjs } from "../mathjs"; -import { MathNode } from "mathjs"; +import { all, create, MathNode } from "mathjs"; import { sort } from "../node-sort"; import { compareEquations } from "./compare-equations"; import { compareCompoundInequations } from "./compare-compound-inequations"; const m: any = mathjs; const log = logger("mv:symbolic"); -const positiveInfinity = 1.497258191621251e6; -const negativeInfinity = -1.497258191621251e6; + +// degrees and gradians are converted with the 355/113 approximation of pi, +// so an angle like 90° lands about 1e-7 quarter turns away from pi/2 instead of on it +const ANGLE_TOLERANCE = 1e-5; + +// angles are evaluated as plain numbers, the Fraction config used elsewhere cannot represent pi +const numeric: any = create(all); export type SymbolicOpts = {}; @@ -70,8 +75,87 @@ export const simplify = (v) => { return ms(v, rules); //.concat(SIMPLIFY_RULES)); }; +// evaluates a constant expression as a plain number, undefined if it has variables +const evaluateNumber = (node: MathNode): number | undefined => { + try { + const value = numeric.evaluate(node.toString()); + return typeof value === "number" ? value : undefined; + } catch (e) { + return undefined; + } +}; + +// returns k when the constant angle is (within tolerance) a multiple k of pi/2 +const quarterTurns = (angle: MathNode): number | undefined => { + const value = evaluateNumber(angle); + + if (value === undefined || !isFinite(value)) { + return undefined; + } + + const turns = value / (Math.PI / 2); + const k = Math.round(turns); + + return Math.abs(turns - k) <= ANGLE_TOLERANCE ? k : undefined; +}; + +// replace trigonometric functions evaluated at an angle where they are zero or undefined +// with their exact value, so that tan(90°) becomes Infinity instead of a large finite number +const snapTrigonometricValues = (node: MathNode) => { + let containsUndefinedValue = false; + let changed = false; + + const r: MathNode | any = node.transform((n: any) => { + if (!n.isFunctionNode || n.args?.length !== 1) { + return n; + } + + const k = quarterTurns(n.args[0]); + + if (k === undefined) { + return n; + } + + const odd = Math.abs(k) % 2 === 1; + let snapped = n; + + switch (n.fn.name) { + case "sin": + snapped = odd ? n : new m.ConstantNode(0); + break; + case "cos": + snapped = odd ? new m.ConstantNode(0) : n; + break; + case "tan": + case "sec": + snapped = odd ? new m.SymbolNode("Infinity") : n; + break; + case "cot": + case "csc": + snapped = odd ? n : new m.SymbolNode("Infinity"); + break; + } + + changed = changed || snapped !== n; + containsUndefinedValue = + containsUndefinedValue || !!snapped.isSymbolNode; + + return snapped; + }); + + // keep the original node when nothing was replaced, later steps rely on its identity + return { r: changed ? r : node, containsUndefinedValue }; +}; + const normalize = (a: string | MathNode | any) => { - let r: string | MathNode | any = a; + const snapped = snapTrigonometricValues(a); + let r: string | MathNode | any = snapped.r; + + // simplify and rationalize treat Infinity as a variable (1 / Infinity + 3 becomes (1 + 3 * Infinity) / Infinity), + // so a constant expression with an undefined trigonometric value is evaluated before it is normalized + const valueWithUndefinedTrigonometry = snapped.containsUndefinedValue + ? evaluateNumber(r) + : undefined; let onlyConstant = true; let containsFunctionNode = false; let containsArrayNode = false; @@ -147,12 +231,17 @@ const normalize = (a: string | MathNode | any) => { log("[normalize] input: ", a.toString(), "output: ", r.toString()); - // check for infinity + // an undefined trigonometric value has no sign, so -tan(90°) is Infinity as well if ( - r.toString() === "Infinity" || - +r.toString() >= positiveInfinity || - +r.toString() <= negativeInfinity + valueWithUndefinedTrigonometry !== undefined && + !isNaN(valueWithUndefinedTrigonometry) ) { + r = isFinite(valueWithUndefinedTrigonometry) + ? new m.ConstantNode(valueWithUndefinedTrigonometry) + : new m.SymbolNode("Infinity"); + } + + if (r.toString() === "Infinity") { r = new m.SymbolNode("Infinity"); } From cbec06ec4f6e252f860e9a9dd491e65791dcc539 Mon Sep 17 00:00:00 2001 From: carlacostea Date: Fri, 30 Oct 2026 09:55:06 +0200 Subject: [PATCH 2/4] test(symbolic): add fixtures for large numbers and undefined trig values (PIE-1418) --- src/fixtures/latex-equal/symbolic/PIE-1418.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/fixtures/latex-equal/symbolic/PIE-1418.ts diff --git a/src/fixtures/latex-equal/symbolic/PIE-1418.ts b/src/fixtures/latex-equal/symbolic/PIE-1418.ts new file mode 100644 index 0000000..8b5e8fe --- /dev/null +++ b/src/fixtures/latex-equal/symbolic/PIE-1418.ts @@ -0,0 +1,53 @@ +// large numbers must be compared by value, only undefined trigonometric values count as infinity +// before PIE-1418 any number of 1,497,258 or more (or -1,497,258 or less) was treated as infinity +export default { + mode: "symbolic", + tests: [ + { + target: "2000000", + eq: ["2\\times10^6", "2000000.0", "\\frac{4000000}{2}"], + ne: ["3000000", "2000001", "1999999", "\\infty", "\\tan(90°)"], + }, + { + target: "-2000000", + eq: ["-2\\times10^6", "-\\frac{4000000}{2}"], + ne: ["-3000000", "2000000", "-\\infty"], + }, + { + // the old cutoff, values on both sides of it + target: "1497259", + ne: ["1497258", "1497260", "1600000", "\\infty"], + }, + { + target: "10^9", + eq: ["1000000000", "10^{9}"], + ne: ["10^{10}", "999999999"], + }, + { + label: "large value with a trigonometric function: $target", + target: "2000000\\sin(90°)", + eq: ["2000000"], + ne: ["3000000", "\\infty"], + }, + { + target: "\\infty", + eq: [ + "\\tan(90°)", + "-\\tan(90°)", + "2\\tan(90°)", + "\\tan(-90°)", + "\\tan(450°)", + "\\tan(3\\pi/2)", + "\\frac{1}{\\cos(90°)}", + "\\frac{1}{\\sin(180°)}", + ], + ne: ["2000000", "-\\infty", "\\tan(89°)", "\\tan(45°)"], + }, + { + label: "undefined trigonometric value in a sum: $target", + target: "3", + eq: ["\\frac{1}{\\tan(90°)}+3", "3+\\cot(90°)"], + ne: ["\\tan(90°)+3"], + }, + ], +}; From 254b28462bf642fa740c0f009cf2ee627826d3f4 Mon Sep 17 00:00:00 2001 From: carlacostea Date: Tue, 3 Nov 2026 15:37:47 +0200 Subject: [PATCH 3/4] =?UTF-8?q?fix(symbolic):=20only=20snap=20exact=20mult?= =?UTF-8?q?iples=20of=2090=C2=B0=20and=20give=20division=20by=20zero=20no?= =?UTF-8?q?=20sign=20(PIE-1418)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tighten the angle tolerance to 2e-7 quarter turns relative to k, so angles that are only close (89.9999°, 1.5708) are no longer undefined. Snap tan and cot to 0 at their zeros, evaluate every constant expression with a snapped value so -1/cos(90°) is unsigned Infinity, and keep indeterminate results such as tan(90°)-tan(90°) as NaN instead of 0. --- src/fixtures/latex-equal/symbolic/PIE-1418.ts | 52 +++++++++++++++++++ src/symbolic/index.ts | 40 +++++++------- 2 files changed, 74 insertions(+), 18 deletions(-) diff --git a/src/fixtures/latex-equal/symbolic/PIE-1418.ts b/src/fixtures/latex-equal/symbolic/PIE-1418.ts index 8b5e8fe..27bf26c 100644 --- a/src/fixtures/latex-equal/symbolic/PIE-1418.ts +++ b/src/fixtures/latex-equal/symbolic/PIE-1418.ts @@ -49,5 +49,57 @@ export default { eq: ["\\frac{1}{\\tan(90°)}+3", "3+\\cot(90°)"], ne: ["\\tan(90°)+3"], }, + { + label: "angles that are only close to a multiple of 90° are not undefined: $target", + target: "\\infty", + eq: ["\\tan(810°)", "\\tan(4590°)", "\\tan(100\\grad)", "\\sqrt{\\tan(90°)}"], + ne: [ + "\\tan(1.5708)", + "\\tan(89.9999°)", + "\\tan(89.9995°)", + "\\tan(\\frac{\\pi}{2}+0.00001)", + "\\csc(3.1416)", + ], + }, + { + target: "\\sin(3.1416)\\cdot10^6", + eq: ["-7.3464"], + ne: ["0"], + }, + { + label: "division by a zero of tan and cot: $target", + target: "\\infty", + eq: [ + "\\frac{1}{\\tan(180°)}", + "\\frac{1}{\\tan(\\pi)}", + "\\cot(180°)", + "\\frac{1}{\\cot(90°)}", + ], + }, + { + target: "0", + eq: ["\\tan(180°)", "\\cot(90°)", "\\cos(90°)\\cdot10^7"], + // indeterminate, not 0 + ne: ["\\tan(90°)-\\tan(90°)", "0\\cdot\\tan(90°)"], + }, + { + label: "division by zero has no sign: $target", + target: "-\\frac{1}{\\cos(90°)}", + eq: ["\\infty", "-\\sec(90°)", "\\frac{1}{\\cos(90°)}"], + ne: ["-\\infty"], + }, + { + target: "\\frac{2}{\\sin(180°)}", + eq: ["\\frac{-2}{\\sin(180°)}", "\\infty"], + }, + { + label: "undefined value next to a variable: $target", + target: "x=\\infty", + eq: ["x=\\tan(90°)"], + }, + { + target: "x+\\infty", + eq: ["x+\\tan(90°)"], + }, ], }; diff --git a/src/symbolic/index.ts b/src/symbolic/index.ts index a4d59ed..48d5cee 100644 --- a/src/symbolic/index.ts +++ b/src/symbolic/index.ts @@ -9,8 +9,9 @@ const m: any = mathjs; const log = logger("mv:symbolic"); // degrees and gradians are converted with the 355/113 approximation of pi, -// so an angle like 90° lands about 1e-7 quarter turns away from pi/2 instead of on it -const ANGLE_TOLERANCE = 1e-5; +// so an angle of k quarter turns (90°, 180°, ...) lands about k * 8.5e-8 quarter turns away from k * pi/2 instead of on it; +// the tolerance is relative to k and leaves a small margin, so angles that are only close, like 89.9999° or 1.5708, are not matched +const ANGLE_TOLERANCE = 2e-7; // angles are evaluated as plain numbers, the Fraction config used elsewhere cannot represent pi const numeric: any = create(all); @@ -96,13 +97,14 @@ const quarterTurns = (angle: MathNode): number | undefined => { const turns = value / (Math.PI / 2); const k = Math.round(turns); - return Math.abs(turns - k) <= ANGLE_TOLERANCE ? k : undefined; + return Math.abs(turns - k) <= ANGLE_TOLERANCE * Math.max(1, Math.abs(k)) + ? k + : undefined; }; // replace trigonometric functions evaluated at an angle where they are zero or undefined // with their exact value, so that tan(90°) becomes Infinity instead of a large finite number const snapTrigonometricValues = (node: MathNode) => { - let containsUndefinedValue = false; let changed = false; const r: MathNode | any = node.transform((n: any) => { @@ -127,33 +129,35 @@ const snapTrigonometricValues = (node: MathNode) => { snapped = odd ? new m.ConstantNode(0) : n; break; case "tan": + snapped = odd ? new m.SymbolNode("Infinity") : new m.ConstantNode(0); + break; + case "cot": + snapped = odd ? new m.ConstantNode(0) : new m.SymbolNode("Infinity"); + break; case "sec": snapped = odd ? new m.SymbolNode("Infinity") : n; break; - case "cot": case "csc": snapped = odd ? n : new m.SymbolNode("Infinity"); break; } changed = changed || snapped !== n; - containsUndefinedValue = - containsUndefinedValue || !!snapped.isSymbolNode; return snapped; }); // keep the original node when nothing was replaced, later steps rely on its identity - return { r: changed ? r : node, containsUndefinedValue }; + return { r: changed ? r : node, changed }; }; const normalize = (a: string | MathNode | any) => { const snapped = snapTrigonometricValues(a); let r: string | MathNode | any = snapped.r; - // simplify and rationalize treat Infinity as a variable (1 / Infinity + 3 becomes (1 + 3 * Infinity) / Infinity), - // so a constant expression with an undefined trigonometric value is evaluated before it is normalized - const valueWithUndefinedTrigonometry = snapped.containsUndefinedValue + // simplify and rationalize treat Infinity as a variable (1 / Infinity + 3 becomes (1 + 3 * Infinity) / Infinity) + // and keep the sign of a division by zero, so a constant expression with a replaced trigonometric value is evaluated before it is normalized + const valueWithSnappedTrigonometry = snapped.changed ? evaluateNumber(r) : undefined; let onlyConstant = true; @@ -231,13 +235,13 @@ const normalize = (a: string | MathNode | any) => { log("[normalize] input: ", a.toString(), "output: ", r.toString()); - // an undefined trigonometric value has no sign, so -tan(90°) is Infinity as well - if ( - valueWithUndefinedTrigonometry !== undefined && - !isNaN(valueWithUndefinedTrigonometry) - ) { - r = isFinite(valueWithUndefinedTrigonometry) - ? new m.ConstantNode(valueWithUndefinedTrigonometry) + // an undefined value has no sign, so -tan(90°) and -1/cos(90°) are Infinity as well; + // an indeterminate result such as tan(90°) - tan(90°) or 0 * tan(90°) is NaN and must not simplify to a number + if (valueWithSnappedTrigonometry !== undefined) { + r = isNaN(valueWithSnappedTrigonometry) + ? new m.SymbolNode("NaN") + : isFinite(valueWithSnappedTrigonometry) + ? new m.ConstantNode(valueWithSnappedTrigonometry) : new m.SymbolNode("Infinity"); } From a98b4c060c313d95d791677edc0951fa63b09f3d Mon Sep 17 00:00:00 2001 From: carlacostea Date: Tue, 3 Nov 2026 16:13:51 +0200 Subject: [PATCH 4/4] =?UTF-8?q?fix(symbolic):=20match=20only=20exact=20mul?= =?UTF-8?q?tiples=20of=2090=C2=B0=20and=20keep=20exact=20values=20next=20t?= =?UTF-8?q?o=20replaced=20zeros=20(PIE-1418)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fixtures/latex-equal/symbolic/PIE-1418.ts | 45 +++++++++++++- src/symbolic/index.ts | 60 ++++++++++++++----- 2 files changed, 89 insertions(+), 16 deletions(-) diff --git a/src/fixtures/latex-equal/symbolic/PIE-1418.ts b/src/fixtures/latex-equal/symbolic/PIE-1418.ts index 27bf26c..268c727 100644 --- a/src/fixtures/latex-equal/symbolic/PIE-1418.ts +++ b/src/fixtures/latex-equal/symbolic/PIE-1418.ts @@ -63,8 +63,49 @@ export default { }, { target: "\\sin(3.1416)\\cdot10^6", - eq: ["-7.3464"], - ne: ["0"], + ne: ["0", "\\infty"], + }, + { + label: "angles far from a multiple of 90° are not matched, however large: $target", + target: "\\infty", + ne: ["\\tan(9090.001°)", "\\tan(90089.99°)", "\\tan(1572.367)"], + }, + { + target: "0", + ne: ["\\tan(10000000)", "\\tan(3999999)"], + }, + { + label: "a replaced zero keeps exact fractions exact: $target", + target: "\\frac{1}{3}", + eq: ["\\sin(0)+\\frac{1}{3}", "\\frac{1+\\sin(0)}{3}"], + ne: ["\\sin(0)+0.3333"], + }, + { + target: "-\\frac{1}{3}", + eq: ["\\sin(0)-\\frac{1}{3}"], + }, + { + target: "\\frac{5}{6}", + eq: ["\\tan(0°)+\\frac{5}{6}"], + }, + { + label: "letters next to a replaced zero are variables, not constants: $target", + target: "\\phi", + eq: ["\\sin(0)+\\phi"], + ne: ["\\sin(0)+1.618"], + }, + { + target: "e", + eq: ["\\sin(0)+e"], + }, + { + label: "indeterminate results are not equal to each other: $target", + target: "\\tan(90°)-\\tan(90°)", + ne: ["\\tan(90°)-\\tan(90°)+7", "0\\cdot\\tan(90°)"], + }, + { + target: "\\frac{\\tan(90°)}{\\tan(90°)}", + ne: ["1", "\\frac{\\tan(90°)}{\\tan(90°)}+5"], }, { label: "division by a zero of tan and cot: $target", diff --git a/src/symbolic/index.ts b/src/symbolic/index.ts index 48d5cee..f69bf86 100644 --- a/src/symbolic/index.ts +++ b/src/symbolic/index.ts @@ -8,10 +8,11 @@ import { compareCompoundInequations } from "./compare-compound-inequations"; const m: any = mathjs; const log = logger("mv:symbolic"); -// degrees and gradians are converted with the 355/113 approximation of pi, -// so an angle of k quarter turns (90°, 180°, ...) lands about k * 8.5e-8 quarter turns away from k * pi/2 instead of on it; -// the tolerance is relative to k and leaves a small margin, so angles that are only close, like 89.9999° or 1.5708, are not matched -const ANGLE_TOLERANCE = 2e-7; +// an angle counts as k quarter turns (90°, 180°, ...) only when it is one up to floating-point error; +// degrees and gradians may have been converted with the 355/113 approximation of pi, so both values of pi are checked, +// which keeps angles that are only close, like 89.9999°, 1.5708 or 9090.001°, from being matched +const ANGLE_TOLERANCE = 1e-9; +const PI_APPROXIMATIONS = [Math.PI, 355 / 113]; // angles are evaluated as plain numbers, the Fraction config used elsewhere cannot represent pi const numeric: any = create(all); @@ -76,8 +77,24 @@ export const simplify = (v) => { return ms(v, rules); //.concat(SIMPLIFY_RULES)); }; -// evaluates a constant expression as a plain number, undefined if it has variables +const hasSymbol = (node: MathNode, matches: (name: string) => boolean) => { + let found = false; + + // the name of a function such as sqrt is a symbol too, under the path "fn" + node.traverse((n: any, path: string) => { + found = found || (n.isSymbolNode && path !== "fn" && matches(n.name)); + }); + + return found; +}; + +// evaluates a constant expression as a plain number, undefined if it has variables; +// any symbol other than pi and Infinity is a variable, even names mathjs knows as constants such as e, E or phi const evaluateNumber = (node: MathNode): number | undefined => { + if (hasSymbol(node, (name) => name !== "pi" && name !== "Infinity")) { + return undefined; + } + try { const value = numeric.evaluate(node.toString()); return typeof value === "number" ? value : undefined; @@ -94,12 +111,16 @@ const quarterTurns = (angle: MathNode): number | undefined => { return undefined; } - const turns = value / (Math.PI / 2); - const k = Math.round(turns); + for (const pi of PI_APPROXIMATIONS) { + const turns = value / (pi / 2); + const k = Math.round(turns); - return Math.abs(turns - k) <= ANGLE_TOLERANCE * Math.max(1, Math.abs(k)) - ? k - : undefined; + if (Math.abs(turns - k) <= ANGLE_TOLERANCE * Math.max(1, Math.abs(k))) { + return k; + } + } + + return undefined; }; // replace trigonometric functions evaluated at an angle where they are zero or undefined @@ -156,10 +177,14 @@ const normalize = (a: string | MathNode | any) => { let r: string | MathNode | any = snapped.r; // simplify and rationalize treat Infinity as a variable (1 / Infinity + 3 becomes (1 + 3 * Infinity) / Infinity) - // and keep the sign of a division by zero, so a constant expression with a replaced trigonometric value is evaluated before it is normalized - const valueWithSnappedTrigonometry = snapped.changed - ? evaluateNumber(r) - : undefined; + // and keep the sign of a division by zero, so a constant expression with an Infinity or a division by zero is evaluated before it is normalized; + // other constant expressions keep their exact value, such as 1/3 in sin(0) + 1/3 + const snappedValue = snapped.changed ? evaluateNumber(r) : undefined; + const valueWithSnappedTrigonometry = + snappedValue !== undefined && + (!isFinite(snappedValue) || hasSymbol(r, (name) => name === "Infinity")) + ? snappedValue + : undefined; let onlyConstant = true; let containsFunctionNode = false; let containsArrayNode = false; @@ -261,6 +286,8 @@ const normalize = (a: string | MathNode | any) => { return r; }; +const isIndeterminate = (node: any) => node?.isSymbolNode && node.name === "NaN"; + export const isMathEqual = (a: any, b: any) => { let as: MathNode; let bs: MathNode; @@ -295,6 +322,11 @@ export const isMathEqual = (a: any, b: any) => { log("[isMathEqual]", as.toString(), "==?", bs.toString()); + // an indeterminate result such as tan(90°) - tan(90°) is not equal to anything, not even another indeterminate result + if (isIndeterminate(as) || isIndeterminate(bs)) { + return false; + } + const isSortingEnough = sort(a).equals(sort(b)); const isTexEnough = as.toTex().trim() === bs.toTex().trim();