Skip to content

MDL writer: format_f64 emits "inf"/"-inf" for infinity values #325

Closed

Description

@bpowers

Description

format_f64() in src/simlin-engine/src/mdl/writer.rs (line 602-608) handles NaN correctly (falls through to Rust's default Display formatting which produces NaN) but has no special-case handling for positive or negative infinity. When f64::INFINITY or f64::NEG_INFINITY is passed in, Rust's Display trait emits inf and -inf respectively.

Vensim does not recognize inf/-inf as valid numeric literals. MDL files typically represent these cases using :NA: or very large sentinel numbers (e.g., 1e+38).

Current code:

fn format_f64(v: f64) -> String {
    if v == v.trunc() && v.abs() < 1e15 {
        format!("{}", v as i64)
    } else {
        format!("{}", v)
    }
}

The v.abs() < 1e15 guard prevents the integer fast path for infinity (since f64::INFINITY.abs() is infinity, not less than 1e15), so it falls through to the else branch which emits inf.

Why it matters

  • Vensim compatibility: MDL files containing inf would fail to load in Vensim, breaking the roundtrip contract of the MDL writer.
  • Correctness: Even though infinity values are unlikely in practice (they could arise from division by zero in graphical function scales or other edge cases), the writer should produce valid MDL output for all possible floating-point inputs.

Component(s) affected

  • simlin-engine -- specifically src/simlin-engine/src/mdl/writer.rs, the format_f64() function

Possible approaches

  1. Add explicit checks for v.is_infinite() before the existing logic, emitting :NA: (or a Vensim-compatible large number like 1e+38) for positive infinity and -1e+38 (or :NA:) for negative infinity.
  2. Investigate what Vensim actually writes in these cases by examining real .mdl files and match that behavior.

Context

Identified during review of PR #324 (MDL writer implementation). The NaN case is already handled implicitly (Rust emits NaN which may also need attention), but infinity is the more immediately concerning case since it produces output Vensim cannot parse.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions