Skip to content

Widen Mod to (m, n, d?): Mod(m, n, d) is d + Mod(m − d, n) - #658

Closed
enumeratio wants to merge 1 commit into
cortex-js:mainfrom
enumeratio:mod-offset
Closed

enumeratio wants to merge 1 commit into
cortex-js:mainfrom
enumeratio:mod-offset

Conversation

@enumeratio

Copy link
Copy Markdown
Contributor

Mod(m, n, d) is the representative of m modulo n in [d, d + n), as in Mathematica's Mod[m, n, d]. It was an unexpected-argument error. This is option 2 of #548.

Evaluation. The signature is (real, real, real?) -> real, and the value is d + Mod(m − d, n) under evaluate() and .N(), through the two-operand handler (so the exact-rational, float and zero-modulus rules apply as before):

before after
Mod(7, 3, 1) error 1
Mod(9, 3, 1) error 3
Mod(-1, 3, 1) error 2
Mod(10, 4, -1) error 2
Mod(7/2, 2, 1/2) error 3/2
Mod(7.5, 3, 1) error 1.5
Mod(x, 3, 1) error unevaluated

The type handler takes d into account (Mod(n, 3, 1/2) is rational, not integer), and sgn makes no claim for the three-operand form.

Declined routes. The JavaScript, GLSL and interval targets and the bigint mode already refuse a Mod with three operands (Mod with 3 operands), so compile() falls back to the interpreter and gives its value. Tests pin this for each. Python is not a built-in target here. The enclosure lowering, Enclosure arithmetic and the map-exact proof already check for two operands. Three places did not, and now decline:

  • the derivative: D(Mod(u, c, d), x) stays D(...) (it was the opaque Apply(Derivative(Mod, 1, 0, 0), …)), and is 0 when nothing depends on x;
  • measurement arithmetic: Mod(7 ± 0.1, 3, 1) stays unevaluated (the sawtooth rule ignores d);
  • the exact-jump broadcast under .N(), which treats every element near a jump for the three-operand form.

The limit code already requires two operands, so Limit of a three-operand Mod stays unevaluated.

LaTeX. The infix \bmod serializer already falls back to the function form for other than two operands, so the offset is kept: \mathrm{Mod}(2x, 3, 1), which parses back to the same expression. \operatorname{mod}(7, 3, 1) parses to Mod(7, 3, 1).

Epsil. The infix % is for exactly two operands: 7 % 3 % 1 parses as Mod(Mod(7, 3), 1), so Mod(m, n, d) is written as a call, Mod(7, 3, 1), which parses back to the same expression. The round-trip corpus gains the two-operand, literal-offset and symbolic-offset cases.

No evaluateMod hook is added; this keeps to the #527 item 3 separation.

OPERATORS.json and library.md are updated by hand for the signature and an example.

Tests. item-548-mod-offset.test.ts: exact, rational and float cases under evaluate() and .N(), the interval property over a grid, symbolic operands, the type, broadcast, each declined route (compile throws with fallback: false, and with fallback: true runs to the interpreter's value), LaTeX and Epsil. The two-operand Mod tests (arithmetic, compile-mod-fast-path, derivatives, enclosure, measurement, jump and limit suites) pass unchanged.

Fixes #548

…ue in [d, d + n), under evaluate() and .N(); the compile targets, enclosure, measurement, limit and derivative code decline the three-operand form; Epsil writes it as a call; add tests. Fix cortex-js#548
@arnog

arnog commented Nov 23, 2026

Copy link
Copy Markdown
Member

Landed in f42a02d, with fixes made while landing (see the commit message). Thanks!

@arnog arnog closed this Nov 23, 2026
arnog added a commit that referenced this pull request Nov 23, 2026
…(m, n, d), the optional combinatorics library, PiecewiseExpand

- #657: @cortex-js/compute-engine/runtime exports sysHelpers, the stateless
  helpers that compiled code calls as _SYS.<name> (#527 item 1).
- #658: Mod(m, n, d) is d + Mod(m - d, n), in [d, d + n) (#548). Every
  compile target declines the three-operand form; the derivative, limit,
  enclosure and measurement code leave it unevaluated. Fixed on landing:
  .N() reads exact operands exactly as the two-operand Mod does (exact
  cancellation of d, a symbol with an exact value near a jump, a dividend
  past the range of a double, no second evaluation of an impure operand);
  exact radicals stay exact; the type is real only for real operands.
- #659: the optional combinatorics entry, loadCombinatorics(ce, { areas }),
  with about 350 permutation and 195 integer-partition statistics checked
  against FindStat (#531). Fixed on landing: the report of a re-load, a
  skipped head declared once the engine's definition is removed, the CI
  comment, string nodes without source offsets in compiled.ts, the synonym
  head LargestCycleLength removed (LongestCycleLength stays), one compiler
  decline test per area, and notes on list literals and time limits.
- #660: PiecewiseExpand rewrites Abs, Sign, Min, Max and Heaviside into a
  Which (#516). Fixed on landing: an argument declared complex or number is
  not split; the arm limit is checked before the product is built; Max()
  and Min() with no operand; exact conditions between radicals; an impure
  expression is returned unchanged; the lazy-operator list and the search
  ranking test.
- Issues CE-1734, CE-1735, CE-1736 filed from the decline census of #638.

Co-authored-by: enumeratio <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Mod with an offset: Mod(m, n, d)

2 participants