You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
engine: MOD with a negative divisor ignores the divisor's sign #610
MOD with a negative divisor returns a non-negative result, but the XMILE spec says the result takes the sign of the divisor.
The engine uses Rust's rem_euclid, which always returns a value in [0, |b|); the spec's floored modulus is a - b * floor(a / b).
Example
expr
engine (rem_euclid)
XMILE floored modulus
-10 MOD 3
2
2
10 MOD -3
1
-2
-10 MOD -3
2
-1
Spec: docs/reference/xmile-v1.0.html, section 3.3.1: "Modulo is defined to return the floored modulus proposed by Knuth. In this form, the sign of the result always follows the sign of the divisor."
Footnote 7 also requires INT to be the floor, which the engine does.
Vensim's INTEGER and MODULO truncate toward zero instead; MDL import already maps them to TRUNC and REM, and test/test-models/tests/rounding/test_rounding.mdl matches Vensim (simulates_rounding_mdl).
The XMILE copy of that model stays disabled on purpose: xmutil writes Vensim's INTEGER as INT.
Why it matters
Only native XMILE models with a negative divisor are affected.
No known model uses one.
Fix
Use a - b * (a / b).floor() everywhere MOD is evaluated (VM, wasm backend, constant folding) and update the unit tests that pin rem_euclid.
Re-surfaced during code review on branch round-builtin (adding the ROUND builtin); pre-existing behavior unrelated to ROUND.
Two additions to the write-up above:
Importer angle: the MDL importer's rename table maps INTEGER -> INT, so every imported Vensim model hits this silently: INTEGER(-9.9) imports as int(-9.9) = -10 where Vensim produces -9 (ground truth: test/test-models/tests/rounding/output.tab, test integer row; vensim.com's fn_integer page says INTEGER truncates toward zero, explicitly contrasting with floor). The MCP skill doc src/simlin-mcp/src/skills/vensim-equation-syntax.md now discloses the divergence, but the runtime gap remains.
A cheaper fix option than the opt-in compat mode (approach 2c above): fix this at import time rather than in the engine. Lower INTEGER(x) in the MDL importer to a truncation-semantics expression (a sign-aware form over the existing builtins, or add a trunc builtin and map INTEGER to it). That preserves XMILE-compliant INT=floor for native models while making imported .mdl provenance match Vensim, without a global mode switch. The same trick could cover MODULO via the identity a - b*trunc(a/b).
changed the title [-]engine: INT/MOD negative-number semantics diverge from Vensim (blocks tests/rounding); MOD uses rem_euclid not XMILE floored modulus[/-][+]engine: MOD with a negative divisor ignores the divisor's sign[/+]on Oct 12, 2026
Activity
engine: WebAssembly simulation backend with full VM parity (#620)
bpowers commentedon Aug 10, 2026
Re-surfaced during code review on branch
round-builtin(adding the ROUND builtin); pre-existing behavior unrelated to ROUND.Two additions to the write-up above:
Importer angle: the MDL importer's rename table maps
INTEGER->INT, so every imported Vensim model hits this silently:INTEGER(-9.9)imports asint(-9.9)= -10 where Vensim produces -9 (ground truth:test/test-models/tests/rounding/output.tab,test integerrow; vensim.com's fn_integer page says INTEGER truncates toward zero, explicitly contrasting with floor). The MCP skill docsrc/simlin-mcp/src/skills/vensim-equation-syntax.mdnow discloses the divergence, but the runtime gap remains.A cheaper fix option than the opt-in compat mode (approach 2c above): fix this at import time rather than in the engine. Lower
INTEGER(x)in the MDL importer to a truncation-semantics expression (a sign-aware form over the existing builtins, or add atruncbuiltin and map INTEGER to it). That preserves XMILE-compliantINT=floor for native models while making imported.mdlprovenance match Vensim, without a global mode switch. The same trick could cover MODULO via the identitya - b*trunc(a/b).engine: address codex and claude PR review comments
engine: add ROUND builtin with round-half-to-even semantics (#1015)
[-]engine: INT/MOD negative-number semantics diverge from Vensim (blocks tests/rounding); MOD uses rem_euclid not XMILE floored modulus[/-][+]engine: MOD with a negative divisor ignores the divisor's sign[/+]