Skip to content

expression-parsing: add read_written_number for significant figures and decimal places - #12

Draft
m-messer wants to merge 1 commit into
mainfrom
feat/number-precision-helpers
Draft

m-messer wants to merge 1 commit into
mainfrom
feat/number-precision-helpers

Conversation

@m-messer

Copy link
Copy Markdown
Member

Ports the number-string half of compareExpressions#273 and #277 (sig_figs / decimal_places) to expression_parsing. The grading (sig_figs_match, decimal_places_match, the tolerances) and the feedback stay in compareExpressions.

expression_parsing

  • read_written_number(string) returns a WrittenNumber (integer, fraction, has_point, exponent), or None if string isn't exactly one decimal number.
  • .significant_figures skips leading zeros. It counts trailing zeros only after a written point: 540 has 2, 540. has 3, and 92.00 has 4.
  • .decimal_places subtracts the exponent: 5.02e4 has 0, and 5e-3 has 3.
  • It's more lenient than is_number, because it reads what a learner wrote: a + sign, leading zeros and a trailing point are accepted. The exponent can be written e, E, *10^ or *10**; #273/#277 only read e/E.
  • round_to_significant_figures(value, figures) returns a float.
  • round_to_decimal_places(value, places) returns a string that keeps its trailing zeros.

Verification

  • ruff, format and mypy are clean. The package tests pass (tests/test_written_number.py is new), and so does the parity check.

🤖 Generated with Claude Code

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant