Skip to content

Calculator: constants, molar mass, text functions, bracketed phrases and a faster engine - #1928

Open
list-jonas wants to merge 8 commits into
abue-ammar:mainfrom
list-jonas:calculator-expressions
Open

list-jonas wants to merge 8 commits into
abue-ammar:mainfrom
list-jonas:calculator-expressions

Conversation

@list-jonas

@list-jonas list-jonas commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Related issue

Closes #754
Closes #85 (the % spelling: 10 % 3, 17 % 5, 2 + 10 % 3; mod already worked)

What changed

New calculator inputs:

  • Dates: today - 1.1.2026 + 3, week 44 2026
  • Math: 12 % 5 (remainder when a plain number follows %), 1.75 as improper / as mixed
  • Money: 15% decrease from 813 (and increase)
  • Science: speed of light in km/s and ~20 other constants, molar mass of C6H12O6
  • Units: 5:00 min/km in km/h, 4° in nautical miles, ~35 new units (eV, ångström, therm, mach, decade, grain, …)
  • Text: first 10 characters of "hello", length of "…", "…" in uppercase
  • Brackets: a phrase in brackets can be used in arithmetic: (15% tip on $80) / 2, (today - 1.1.2026) / 2, (c4 to hz) * 2. (2m) shows 2 m. The card dims brackets like operators.

New code lives in CalcScience and CalcText. A one-pass scan for a trigger word (or a quote mark) lets other queries skip them.

The calculator is also faster:

  • ASCII fast paths replace Unicode property lookups.
  • Short numbers are parsed without building a String.
  • The expression is echoed back in one pass.
  • The time-zone grammar is skipped for queries without letters or a clock.
Benchmark (µs/query) Before After
Arithmetic 9.3 6.3
Units 16.2 12.4
Phrases 28.7 23.1
App searches 2.6 2.0
Typing, per keystroke 16.4 13.9

Memory footprint

Step Memory
Idle after launch
Palette open
Feature in use (peak)
Palette closed, settled

Leak-tested:

Demo

Drawbacks

  • 10 percent 3 and 5% 2 now give a remainder, because the tokenizer can't tell percent or 5% from %. Percentages still work as before: 10% of 89 is 8.9, 89 + 10% is 97.9 and 10% off 89 is 80.1.
  • A bracketed phrase is solved in two extra passes, so (today - 1.1.2026) / 2 takes ~80 µs.
  • The constant and atomic-weight tables are hand-curated: CODATA 2018 and IUPAC.

Tests & validation

  • ./Scripts/run-tests.sh CalcTests passes, with new cases for each feature and for the edge cases.
  • ./Scripts/lint.sh is clean and the Debug build has 0 new warnings.
  • A differential run of 120k queries across 6 number formats (720k results) against main lost no answers. Every change is a new answer, or an error card on fuzz garbage that now shows nothing.
  • Benchmarked with CalcPerformanceTests plus a keystroke-by-keystroke typing test.

…re units

- `12 % 5` is a remainder when a plain number follows the `%`
- `today - 1.1.2026 + 3`, `week 44 2026`
- `1.75 as improper` / `as mixed`, `15% decrease from 813`
- `speed of light in km/s`, `molar mass of C6H12O6`, `5:00 min/km in km/h`
- `first 10 characters of "hello"` and other quoted-text functions
- About 35 new units (eV, ångström, therm, mach, decade, grain, …)
… paths

- ASCII fast paths for whitespace and digit checks in the tokenizer and word splits
- Echo the expression in one pass instead of split + two Foundation replaces
- Parse short integers arithmetically instead of through a String
- Skip the time-zone grammar for queries with no letters or clock
@github-actions

Copy link
Copy Markdown

Thanks for the PR! A maintainer will review it soon.

@list-jonas
list-jonas marked this pull request as ready for review November 15, 2026 22:15

This branch has not been deployed

No deployments
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.

[Bug]: Inline calculator does not support mod operation [Feature]: Modulo operator in the inline calculator

1 participant