Skip to content

Parse scientific notation in calc() expressions#2562

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-calc-scientific-notation
Draft

Parse scientific notation in calc() expressions#2562
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-calc-scientific-notation

Conversation

@posthog

@posthog posthog Bot commented Jul 2, 2026

Copy link
Copy Markdown

Summary

Fixes a real runtime error where valid near-zero PCB coordinates broke SmtPad placement.

The calc() tokenizer in lib/utils/evaluateCalcString.ts only consumed digits and decimal points when scanning a number. When a coordinate came out as floating-point noise near zero and got stringified in scientific notation (e.g. 1.1368683772161603e-13mm), the scanner stopped at the e, then treated the leftover e as a unit — throwing Unknown unit: "e". That bubbled up through _validatePcbCoordinateReferences and surfaced to users as Invalid pcbY value for SmtPad: Invalid calc() expression.

The fix teaches the number scanner to consume an optional exponent ([eE][+-]?digits) before reading a unit, so scientific-notation values parse as plain numbers. The exponent is only consumed when actually followed by (optional sign +) digits, so identifiers and stray es are unaffected.

Why

Error tracking showed this firing repeatedly across multiple users and sessions over a couple of days — a genuine defect on valid input, not user mistake. It breaks coordinate resolution on affected pads whenever a legitimate near-zero value happens to render in scientific notation.

Test plan

  • Added tests/utils/evaluate-calc-string-scientific-notation.test.ts covering the reported value, positive/negative/signed exponents, scientific notation inside calc() with arithmetic, and that plain numbers/units still parse.
  • Existing calc-pcb-position* suites still pass; typecheck clean.

Created with PostHog Code from an inbox report.

The calc-expression tokenizer only consumed digits and decimal points when
scanning a number. A near-zero PCB coordinate that stringifies in scientific
notation (e.g. "1.1368683772161603e-13mm") caused the scanner to stop at the
"e", then treat "e" as a unit and throw "Unknown unit: e" — surfacing to users
as "Invalid pcbY value for SmtPad: Invalid calc() expression".

Teach the number scanner to consume an optional exponent ([eE][+-]?digits)
before reading a unit, so scientific-notation values parse as plain numbers.

Generated-By: PostHog Code
Task-Id: 1ba46a7c-5db7-4040-ae55-c65c320bda42
@vercel

vercel Bot commented Jul 2, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
tscircuit-core-benchmarks Ready Ready Preview, Comment Jul 2, 2026 10:15pm

Request Review

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has had no recent activity. It will be closed if no further activity occurs.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants