Skip to content

engine: MOD with a negative divisor ignores the divisor's sign #610

Open

Description

@bpowers

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.

Pointers

  • src/simlin-engine/src/vm.rs (eval_op2, Op2::Mod)
  • src/simlin-engine/src/compiler/fold.rs
  • src/simlin-engine/src/wasmgen/lower_tests.rs (op2_mod_matches_rem_euclid_all_sign_combos)

Activity

  1. added
    engine
    rust
    on May 21, 2026
  2. bpowers commented on Aug 10, 2026

    @bpowers
    OwnerAuthor

    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:

    1. 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.

    2. 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).

  3. added
    bug
    on Oct 12, 2026
  4. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugenginerust

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions