Skip to main content

A tolerance check can accept NaN

Updated 2 min readMarkdown
A tolerance check can accept NaN

“No error exceeded tolerance” can pass an invalid value. In an authored Python example with reference 1.0, candidate NaN and absolute tolerance 0.125, both ordered comparisons are false:

error > tolerance   # False
error <= tolerance  # False

The rejection-only expression not (error > tolerance) therefore returns true. Under this worksheet’s finite-only contract, that candidate must be rejected.

The policy is explicit: reference and candidate must both be finite, then the absolute error must be at most 0.125. Relative tolerance is zero. A nonfinite reference is a fixture error, not an eligible failed candidate. APIs that intentionally allow NaN or infinity require a different contract.

The saved worksheet retains all nine authored rows. Seven have a valid reference: exact equality, both tolerance boundaries, one finite value outside the boundary, NaN and both infinities. The finite-only contract accepts the three intended finite cases. The rejection-only predicate accepts those three plus NaN. Two invalid-reference fixtures remain outside the candidate denominator.

Python’s value-comparison documentation explains the built-in float behavior. math.isfinite tests validity. math.isclose never considers NaN close; infinities are close only to themselves. A closeness result does not replace a finite-only reference policy.

One eighth and the finite fixture values are binary-exact, so this example avoids a separate decimal-rounding question. The result records every input, error, predicate and role. Special values are string tags, keeping the output strict JSON.

One reviewed local standard-library invocation retained all nine rows and thirteen passing controls. The original program, fixture, protocol and full result are available as an MIT proof package. No model, GPU or existing grader was run.

For this scalar contract, validate the reference, validate the candidate, then check the positive tolerance relation. Arrays still need explicit shape, dtype, broadcasting and reduction rules. This worksheet does not establish general numerical equivalence, a vulnerability in any real benchmark, or the prevalence of invalid model outputs.

Read the complete Nodus note and download the proof.

About the author

No comments yet. Be the first to comment.