diff --git a/src/conversion/__tests__/latex-to-ast.spec.ts b/src/conversion/__tests__/latex-to-ast.spec.ts index 183e77e..00463b6 100644 --- a/src/conversion/__tests__/latex-to-ast.spec.ts +++ b/src/conversion/__tests__/latex-to-ast.spec.ts @@ -2,10 +2,10 @@ import { LatexToAst } from "../latex-to-ast"; // import { latexParser } from "latex-utensils"; // conversion of degrees in radians - angle configuration -const degree = 355 / (180 * 113); +const degree = Math.PI / 180; //conversion of gradians in radians - angle configuration -const gradian = 355 / (200 * 113); +const gradian = Math.PI / 200; const fixtures = [ //parentheses are stripped! diff --git a/src/conversion/latex-to-ast.ts b/src/conversion/latex-to-ast.ts index b9987d9..5bdaf2d 100644 --- a/src/conversion/latex-to-ast.ts +++ b/src/conversion/latex-to-ast.ts @@ -1168,7 +1168,7 @@ export class LatexToAst { this.advance(); } else if (this.token.token_type === "deg") { // conversion of degrees in radians - angle configuration - const degree = 355 / (180 * 113); + const degree = Math.PI / 180; // @ts-ignore result = ["*", degree]; @@ -1176,7 +1176,7 @@ export class LatexToAst { this.advance(); } else if (this.token.token_type === "grad") { //conversion of gradians in radians - angle configuration - const gradian = 355 / (200 * 113); + const gradian = Math.PI / 200; // @ts-ignore result = ["*", gradian]; diff --git a/src/fixtures/latex-equal/symbolic/PIE-905.ts b/src/fixtures/latex-equal/symbolic/PIE-905.ts new file mode 100644 index 0000000..ef7d86b --- /dev/null +++ b/src/fixtures/latex-equal/symbolic/PIE-905.ts @@ -0,0 +1,90 @@ +// numbers that differ from the target by less than 0.00005 must not be scored as equal +export default { + mode: "symbolic", + tests: [ + { + target: "800", + eq: ["800.0", "800.00", "400*2", "\\frac{1600}{2}"], + ne: ["800.00001", "799.99999", "800.0001"], + }, + { + target: "800", + mode: "literal", + ne: ["800.00001"], + }, + { + target: "-800", + eq: ["-800.0", "-400*2"], + ne: ["-800.00001", "-799.99999"], + }, + { + target: "100", + eq: ["10^2", "100.000"], + ne: ["100.000001"], + }, + { + target: "10^2", + eq: ["100"], + ne: ["100.00001"], + }, + { + target: "1000000", + ne: ["1000000.0001"], + }, + { + target: "\\sqrt{2}", + eq: ["\\frac{2}{\\sqrt{2}}", "\\sin(45°)\\cdot 2"], + ne: ["1.4142", "1.41421356237"], + }, + { + target: "\\frac{\\sqrt{3}}{2}", + eq: ["\\sin(60°)", "\\cos(30°)", "\\sin(\\frac{\\pi}{3})"], + ne: ["0.866", "0.86603"], + }, + // values below 1 need the same number of matching digits as larger ones + { + target: "\\frac{1}{3}", + eq: ["\\frac{2}{6}"], + ne: ["0.333333333333", "0.3333"], + }, + { + target: "\\frac{1}{7}", + ne: ["0.142857142857", "0.142857"], + }, + { + target: "0", + eq: ["\\sin(2\\pi)", "\\cos(270°)", "\\sin(3600°)"], + ne: ["0.0000000000001", "0.00001"], + }, + // floating-point noise from pi and angle conversions is still accepted + { + target: "0.5", + eq: ["\\sin(30°)", "\\cos(60°)", "-\\cos(\\frac{2\\pi}{3})"], + }, + { + target: "\\sqrt{3}", + eq: ["\\tan(60°)"], + }, + { + target: "0.3", + eq: ["0.1+0.2"], + }, + { + target: "1000", + eq: ["1000\\tan(45°)"], + }, + // angles in degrees, radians and gradians are converted with the exact value of pi + { + target: "30°", + eq: ["\\arcsin(0.5)"], + }, + { + target: "\\frac{\\pi}{2}", + eq: ["90°", "100\\grad"], + }, + { + target: "\\pi", + eq: ["180°"], + }, + ], +}; diff --git a/src/fixtures/latex-equal/symbolic/math-evaluator.ts b/src/fixtures/latex-equal/symbolic/math-evaluator.ts index 137ba45..ea69995 100644 --- a/src/fixtures/latex-equal/symbolic/math-evaluator.ts +++ b/src/fixtures/latex-equal/symbolic/math-evaluator.ts @@ -13,7 +13,7 @@ export default { "10*10", "100 / 2 + 50", ], - // ne: ["100.000001", "44 + 57", "50 * 3", "25 * 2 + 51"], + ne: ["100.000001", "44 + 57", "50 * 3", "25 * 2 + 51"], }, { target: "13/2", diff --git a/src/symbolic/index.ts b/src/symbolic/index.ts index 1e14c72..0701a60 100644 --- a/src/symbolic/index.ts +++ b/src/symbolic/index.ts @@ -10,6 +10,14 @@ const log = logger("mv:symbolic"); const positiveInfinity = 1.497258191621251e6; const negativeInfinity = -1.497258191621251e6; +// tolerances for floating-point noise from trig functions, pi and angle conversions (PIE-905); +// the noise grows with the angle, e.g. sin(2pi) = -2.4e-16, sin(200pi) = 3.9e-15, tan(36045°) - 1 = -5.4e-14, +// so a value is accepted only when it agrees with the target to about 13 significant digits +const RELATIVE_EPSILON = 1e-13; +// floor for comparisons with 0, where a relative tolerance alone would require an exact match; +// it covers residues such as sin(200pi) and cos(100.5pi) while still rejecting 0 vs 0.0000000000001 +const ABSOLUTE_EPSILON = 1e-14; + export type SymbolicOpts = {}; const { simplify: ms, rationalize } = mathjs; @@ -156,18 +164,34 @@ const normalize = (a: string | MathNode | any) => { r = new m.SymbolNode("Infinity"); } - if (r.value) { - r.value = new m.Fraction(Math.round(r.value * 10000) / 10000); - } else if (r.fn === "unaryMinus") { - r.args[0].value = new m.Fraction( - Math.round(r.args[0].value * 10000) / 10000 - ); - r = simplify(r); + return r; +}; + +// numeric value of an expression without variables, such as 5, -5 or -1/2; undefined for anything else +const constantValue = (node: any): number | undefined => { + if (node.filter((n) => n.isSymbolNode).length > 0) { + return undefined; } - return r; + try { + const result = node.evaluate(); + + // equations and inequalities without variables evaluate to booleans + if (typeof result === "boolean") { + return undefined; + } + + const value = +result; + return Number.isFinite(value) ? value : undefined; + } catch (e) { + return undefined; + } }; +const areNearlyEqual = (a: number, b: number) => + Math.abs(a - b) < + ABSOLUTE_EPSILON + RELATIVE_EPSILON * Math.max(Math.abs(a), Math.abs(b)); + export const isMathEqual = (a: any, b: any) => { let as: MathNode; let bs: MathNode; @@ -211,6 +235,14 @@ export const isMathEqual = (a: any, b: any) => { return true; } + // compare constant results numerically, allowing only for floating-point noise + const aValue = constantValue(as); + const bValue = constantValue(bs); + + if (aValue !== undefined && bValue !== undefined) { + return areNearlyEqual(aValue, bValue); + } + // if both expressions are equations if (as.fn === "equal" && bs.fn === "equal") { return compareEquations(as, bs, false);