Skip to content

fix(symbolic): stop treating large numbers as infinity (PIE-1418) - #78

Merged
CarlaCostea merged 6 commits into
developfrom
fix/PIE-1418
Nov 4, 2026
Merged

CarlaCostea merged 6 commits into
developfrom
fix/PIE-1418

Conversation

@CarlaCostea

@CarlaCostea CarlaCostea commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

What

In symbolic mode, any answer that worked out to 1,497,258 or more (or −1,497,258 or less) was treated as infinity, so any two large numbers were scored as equal (2000000 vs 3000000). Large numbers are now compared by value. Only trigonometric values that are undefined at an exact multiple of 90° (such as \tan(90°)) and an explicit \infty count as infinity.

Jira: https://illuminate.atlassian.net/browse/PIE-1418

Why it happened

normalize() in src/symbolic/index.ts replaced any constant result at or beyond ±1.497258191621251e6 with the Infinity symbol. That number is tan(90°) when degrees are converted with the 355/113 approximation of π: 90° lands about 1e-7 away from π/2, so tan(90°) comes out as a large finite number rather than infinity. The cutoff was meant to catch those undefined trig values, but it applied to every number.

Change

src/symbolic/index.ts:

  • Before normalizing, trig functions with a constant angle at a multiple k of π/2 are replaced with their exact value:
    • tan/sec at odd k and cot/csc at even k become Infinity.
    • sin, cos, tan and cot at their zeros become 0.
  • Matching the angle: an angle counts as k quarter turns only when it is one up to floating-point error (1e-9·max(1, |k|) quarter turns). Since fix(symbolic): stop accepting near-miss numbers (PIE-905) #76, degrees and gradians convert with Math.PI, so exact multiples of 90° land within that. Angles that are only close don't match, however large k is: 89.9999°, 1.5708, 9090.001°, \tan(10000000).
  • Evaluation: a constant expression whose result is undefined, or that still contains Infinity, is evaluated numerically before normalizing. Other constant expressions keep the normal symbolic path, so exact values stay exact (\sin(0)+\frac{1}{3} = \frac{1}{3}). Only pi and Infinity count as constants. Any other letter is a variable, even names mathjs knows, such as e, E and phi.
    • simplify/rationalize treat Infinity as a variable, for example 1/\tan(90°)+3 becomes (1 + 3·Infinity)/Infinity. They also keep the sign of a division by zero.
    • An undefined value has no sign, so -\tan(90°) and -\frac{1}{\cos(90°)} are both ∞. That keeps 2\tan(90°) = ∞ and \frac{1}{\tan(90°)} = 0 working as before.
    • An indeterminate result (\tan(90°)-\tan(90°), 0\cdot\tan(90°), \frac{\tan(90°)}{\tan(90°)}) becomes NaN, which isMathEqual never treats as equal to anything, including another indeterminate result.
  • Size cutoff removed: a result is treated as infinity only if it actually is Infinity.
  • If no trig value is replaced, the original node is passed on unchanged, because the compound-inequality path depends on it.

Builds on PIE-905 (#76, merged), which moved degree conversion to Math.PI and removed the 4-decimal rounding. develop is merged in. The conflict was in index.ts, where both PRs add helpers in the same place, and it was resolved by keeping both.

Scoring impact

Compared with develop.

Answers that were previously scored correct and are now scored incorrect:

  • Any two different numbers beyond ±1,497,258, for example 2000000 vs 3000000, 2000001, 1999999, and 10^9 vs 10^{10}
  • Large numbers vs infinity, for example 2000000 vs \infty and \tan(90°)
  • Large numbers with opposite signs, -2000000 vs 2000000
  • Numbers just past the old cutoff, 1497259 vs 1497260 and 1600000
  • \infty vs -\infty, because -\infty also fell under the negative cutoff, and -\frac{1}{\cos(90°)} vs -\infty
  • Indeterminate results vs a number or each other: \tan(90°)-\tan(90°) and 0\cdot\tan(90°) vs 0, \frac{\tan(90°)}{\tan(90°)} vs 1, and 0\cdot\tan(90°) vs \frac{0}{0}

Answers that were previously scored incorrect and are now scored correct:

  • Zeros scaled past the 4-decimal rounding: \cos(90°)\cdot10^7 vs 0
  • Undefined values next to a variable: x=\tan(90°) vs x=\infty, x+\tan(90°) vs x+\infty
  • These used to throw a Fraction precision error and now score correct: y=\tan(90°)x vs y=\infty x, and x<\tan(90°) vs x<\infty

Answers whose scoring is unchanged:

  • Every undefined value in degrees-and-gradians.ts is still ∞: \tan(90°), \cot(0°), \csc(180°), \sec(270°), \tan(\pi/2), \tan(100\grad) and the rest.
  • \tan(450°), \tan(-90°), -\tan(90°) and 2\tan(90°) are still ∞.
  • Reciprocals of zeros are still ∞, with no sign: \frac{1}{\cos(90°)}, -\frac{1}{\cos(90°)}, \frac{1}{\tan(180°)}, \frac{1}{\cot(90°)}, and \frac{2}{\sin(180°)} vs \frac{-2}{\sin(180°)}.
  • \frac{1}{\tan(90°)}+3 is still 3.
  • Near angles are still finite: \tan(1.5708), \tan(89.9999°), \tan(9090.001°), \csc(3.1416), and \sin(3.1416)\cdot10^6 ≠ 0.
  • A replaced zero leaves the rest exact: \sin(0)+\frac{1}{3} = \frac{1}{3}, \tan(0°)+\frac{5}{6} = \frac{5}{6}, and \sin(0)+\phi = \phi.
  • All existing fixtures give the same results.

Tests

  • Before (develop, with fix(symbolic): stop accepting near-miss numbers (PIE-905) #76): 1597 passed. After: 1681 passed (+84 fixtures in src/fixtures/latex-equal/symbolic/PIE-1418.ts). tsc --noEmit is clean.
  • Re-enabled or changed fixtures: none. With the new fixtures run against develop's src/symbolic/index.ts, exactly the 24 changed-answer cases fail.

Follow-up

  • Demo page (docs/) to be regenerated after merge.
  • Not fixed here:
    • 0<x<\tan(90°) vs 0<x<\infty threw on develop and is now scored incorrect. The compound-inequality path doesn't go through this normalization yet.
    • Inside equations or next to variables, indeterminate forms still simplify to 0, for example x=0\cdot\tan(90°) vs x=0 and 0\cdot\tan(90°)+x vs x.
    • \infty-\infty without trig is still 0, as on develop.
    • \tan90\deg (no parentheses) parses as tan(90 rad)·deg, a separate parser issue that was already wrong on develop.

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.
…zero no sign (PIE-1418)

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.
@CarlaCostea
CarlaCostea changed the base branch from master to develop November 3, 2026 13:39
Keeps both sides of the conflict in src/symbolic/index.ts: the numeric
comparison helpers from PIE-905 and isIndeterminate from PIE-1418.
Degrees now convert with Math.PI, so angles are matched against Math.PI only.
@CarlaCostea
CarlaCostea merged commit 2124547 into develop Nov 4, 2026
3 checks passed
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 1.4.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant