Skip to content

Support scientific notation in Decimal::from_str()#781

Merged
paupino merged 1 commit intomasterfrom
fix/753-v2-scientific-from-str
Mar 14, 2026
Merged

Support scientific notation in Decimal::from_str()#781
paupino merged 1 commit intomasterfrom
fix/753-v2-scientific-from-str

Conversation

@paupino
Copy link
Copy Markdown
Owner

@paupino paupino commented Mar 14, 2026

Adds support for parsing scientific notation (e.g. 1.23e4, 6.7E-1) directly via Decimal::from_str().

Approach

On error from parse_str_radix_10, checks for e/E in the input and dispatches to from_scientific_lossy. This ensures zero overhead on the happy path - benchmarks show no regression for standard decimal parsing (~73ns before and after).

Alternative approaches considered:

  • Pre-scan with contains: ~2x regression (73ns -> 154ns) due to redundant byte scanning
  • Byte iterator pre-scan: ~70% regression (73ns -> 119ns)
  • Both pre-scan approaches were eliminated due to unacceptable cost on the common case

Tests

Added tests for: positive exponent, negative exponent, uppercase E, negative value with scientific notation, and zero exponent.

Fixes #722. Based on the original work by @kofki in #753.

@paupino paupino force-pushed the fix/753-v2-scientific-from-str branch from eb1b9aa to cca4a84 Compare March 14, 2026 04:21
On error from parse_str_radix_10, check for 'e'/'E' and dispatch to
from_scientific_lossy. Zero overhead on the happy path — benchmarks
show no regression for standard decimal parsing.

Fixes #722.

Co-authored-by: kofki <69553679+kofki@users.noreply.github.com>
@paupino paupino force-pushed the fix/753-v2-scientific-from-str branch from cca4a84 to b84da21 Compare March 14, 2026 04:24
@paupino paupino enabled auto-merge (squash) March 14, 2026 04:26
@paupino paupino merged commit 13b586d into master Mar 14, 2026
15 checks passed
@paupino paupino deleted the fix/753-v2-scientific-from-str branch March 14, 2026 04:28
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.

from_str should support scientific notation

1 participant