Skip to content

NaN and ±Infinity double literals are emitted as undefined identifiers (NaNd, Infinity, -Infinity) in every language except JavaScript #96

Closed

Description

@matt-edmondson

What's wrong

LanguageGeneratorBase.FormatDouble (Coder/Languages/LanguageGeneratorBase.cs) formats with ToString("R") and, as its remarks say, leaves a non-finite value "as it is". CSharpGenerator writes {value.ToString(InvariantCulture)}d (Coder/Languages/CSharpGenerator.cs:105-106). Neither maps NaN or infinity to a spelling the target language understands.

Repro

gen.Generate(Literal.DecimalValue(double.NaN)), and the same for PositiveInfinity and NegativeInfinity:

Target Emitted Result
C# NaNd, Infinityd, -Infinityd not a valid token
C / C++ NaN, Infinity gcc: 'NaN' undeclared
Go NaN undefined: NaN
Rust NaN E0425 cannot find value NaN
Python NaN NameError: name 'NaN' is not defined
JavaScript NaN, Infinity correct

These results come from compiling the generated text with each real toolchain.

Why it matters

NaN and infinity are common sentinel and default values, for example const double Unset = NaN or an initial min = Infinity. Any AST that contains one produces code that fails to compile in 6 of the 7 targets, and nothing warns about it at generation time.

Suggested fix

Add a per-language hook for non-finite values, such as a virtual FormatDouble or a FormatNonFinite(double):

Language NaN +Inf / −Inf
C# double.NaN double.PositiveInfinity / double.NegativeInfinity
C++ std::numeric_limits<double>::quiet_NaN() std::numeric_limits<double>::infinity() (negated)
C NAN INFINITY / -INFINITY (needs <math.h>)
Go math.NaN() math.Inf(1) / math.Inf(-1) (needs import "math")
Rust f64::NAN f64::INFINITY / f64::NEG_INFINITY
Python float("nan") float("inf") / float("-inf")
JavaScript NaN Infinity / -Infinity

The C and Go spellings also need an include or import. If the generators can't add one yet, they should throw NotSupportedException rather than emit an undefined name.

Acceptance: a data-driven test covers NaN, +∞ and −∞ for each generator.

Activity

  1. added
    bug
    on Sep 27, 2026
  2. matt-edmondson commented on Sep 27, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

  3. matt-edmondson commented on Sep 28, 2026

    @matt-edmondson
    ContributorAuthor

    Decision (maintainer, 2026-09-28)

    Next reader: add the per-language non-finite hook and the "required imports" mechanism in SourceFile generation, then add the data-driven NaN/+∞/−∞ test for each generator. Close this issue and #78 together.


    Generated by Claude Code

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

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions