Skip to content

A single Tolerance abstraction for rtol/atol #308

Open

Description

@m-messer

Problem / use case

#289 put the rtol/atol check into one function, values_within_tolerance in app/context/tolerance.py. The way a tolerance is chosen is still spread out:

  1. Two sources of truth. EvaluationParameters.rtol/atol and parsing_parameters["rtol"/"atol"]. _tolerances in physical_quantity/graphs/match.py reads one and checks the other.
  2. "Not set" and 0 look the same. rtol: 0 can't switch off the implicit significant-figures tolerance for physical quantities: 1.04 m vs 1.0 m is graded correct with rtol: 0.
  3. The implicit tolerance applies only to physical quantities. 1.04 m vs 1.0 m passes, but symbolic 1.04 vs 1.0 fails.
  4. The sig-fig formula is duplicated, in the app and in packages expression_parsing/numbers.py (compute_relative_tolerance_from_significant_decimals).
  5. Loose floats are passed through evaluation.py (_apply_tolerance_aliases, _apply_precision_parameters, _build_parameters) and match.py (_tolerances, _within_tolerance).

Proposed solution

A frozen Tolerance dataclass, built once in _build_parameters and passed to both contexts:

@dataclass(frozen=True)
class Tolerance:
    rtol: float | None = None   # None = not set
    atol: float | None = None
    source: str = "default"     # "params", "sig_figs", "decimal_places", "implicit"

    def contains(self, res, ans, unit_factor=1) -> bool: ...
  • None means unset, so rtol: 0 really means exact.
  • Decide whether the implicit sig-fig tolerance applies to symbolic answers too, or to neither context.
  • Use the package's sig-fig helper and remove the app copy.
  • contains replaces values_within_tolerance, and does the unit scaling now in _within_tolerance.

Alternatives considered

Keep the floats and add a sentinel for "unset". That is smaller, but it leaves points 1, 3 and 5 as they are.

Related: #289, #300, #270.


Jira: LAM-323

🤖 Generated with Claude Code

Activity

  1. m-messer commented on Oct 30, 2026

    @m-messer
    MemberAuthor

    Another case for this design, from #285: ordered comparison criteria on physical quantities (<, >, <=, >=) ignore rtol/atol. For example, 1.0 m vs 1.01 m with criteria: "response >= answer" and rtol: 0.05 is graded false. The Tolerance abstraction should define what a tolerance means for an inequality as well as for equality.

    🤖 Generated with Claude Code

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions