Skip to content

Support superscript decimal point and fraction slash in exponents (#2250) - #2330

Merged
andrewgsavage merged 2 commits into
hgrecco:masterfrom
apoorvdarshan:superscript-decimal-fraction-exponents-2250
Jul 26, 2026
Merged

Support superscript decimal point and fraction slash in exponents (#2250)#2330
andrewgsavage merged 2 commits into
hgrecco:masterfrom
apoorvdarshan:superscript-decimal-fraction-exponents-2250

Conversation

@apoorvdarshan

Copy link
Copy Markdown
Contributor

Closes #2250.

Problem

Unit strings using a superscript decimal point (, U+22C5) or a superscript fraction slash (, U+2E0D) inside an exponent raise UndefinedUnitError, even though ordinary superscript exponents already work:

>>> ureg.Unit("gr²")        # grain ** 2        (works)
>>> ureg.Unit("gr⁰⋅³³³")    # UndefinedUnitError: '⋅' is not defined
>>> ureg.Unit("gr¹⸍³")      # UndefinedUnitError: '⸍' is not defined

Fix

As suggested by @andrewgsavage in the issue, this extends the existing superscript handling in string_preprocessor (pint/util.py):

  • _pretty_table now maps . (decimal point) and / (fraction slash).
  • _pretty_exp_re now accepts as a decimal separator and as a fraction separator within a superscript exponent.

Because the exponent is already wrapped as **(...), the fraction stays correctly parenthesized:

>>> ureg.Unit("gr⁰⋅³³³")       # grain ** 0.333
>>> ureg.Unit("gr¹⸍³")         # grain ** 0.3333333333333333   (== 1/3)
>>> ureg.Unit("ft²·gr¹⸍³/lb")  # foot ** 2 * grain ** (1/3) / pound

· (U+00B7, the multiplication interpunct) is a distinct character from (U+22C5) and is unaffected — kg·m/s² still parses as kilogram * meter / second ** 2.

Testing

  • Added TestStringProcessor::test_superscript covering the existing forms (gr², gr⁻³), the interpunct-is-multiplication case (kg·m), and the two new separators. It fails on main and passes with this change.
  • Full test suite (excluding the benchmark dir, which needs pytest-benchmark): 1636 passed, no regressions.
  • ruff check / ruff format --check clean; CHANGES updated.

Disclosure: this change was prepared with the assistance of an AI tool (Claude Code). I reproduced the issue, implemented and verified the fix and tests, ran the suite and linters, and take responsibility for the contribution and will respond to review feedback personally.

apoorvdarshan and others added 2 commits July 3, 2026 22:23
…recco#2250)

Unit strings could not use a superscript decimal point (`⋅`, U+22C5) or
fraction slash (`⸍`, U+2E0D) in exponents, so `gr⁰⋅³³³` and `gr¹⸍³`
raised UndefinedUnitError even though ordinary superscripts (`gr²`) worked.

Map `⋅` to `.` and `⸍` to `/` and extend the superscript exponent regex
to accept them; the existing `**(...)` wrapping keeps the fraction correctly
parenthesized (`gr¹⸍³` -> `gr**(1/3)`). The multiplication interpunct `·`
(U+00B7) is a distinct character and is unaffected. Adds tests.
@andrewgsavage
andrewgsavage merged commit 7f87617 into hgrecco:master Jul 26, 2026
51 checks passed
@codspeed-hq

codspeed-hq Bot commented Jul 26, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 23.22%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 2 improved benchmarks
✅ 446 untouched benchmarks
⏩ 448 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_load_definitions_stage_1[True] 184.1 µs 146.1 µs +26.01%
WallTime test_import 323.9 ms 268.8 ms +20.49%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing apoorvdarshan:superscript-decimal-fraction-exponents-2250 (61643f5) with master (5d9c534)

Open in CodSpeed

Footnotes

  1. 448 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

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.

Superscript period and superscript slash not defined in the unit registry

2 participants