Skip to content

fix: parse scientific notation in pcb coordinate strings#2331

Open
asapsav wants to merge 4 commits into
tscircuit:mainfrom
asapsav:fix/scientific-notation-fails-in-pcb-coords
Open

fix: parse scientific notation in pcb coordinate strings#2331
asapsav wants to merge 4 commits into
tscircuit:mainfrom
asapsav:fix/scientific-notation-fails-in-pcb-coords

Conversation

@asapsav
Copy link
Copy Markdown

@asapsav asapsav commented May 24, 2026

Summary

Footprint primitives with CAD-imported coordinates in scientific notation (e.g. pcbX="-1.1368683772161603e-13mm") were failing with:

source_invalid_component_property_error: Invalid pcbY value for SmtPad:
Invalid calc() expression. expression="1.1368683772161603e-13mm"

This happens when footprints are auto-imported from EasyEDA/JLC/KiCad sources that serialize near-zero values as IEEE float dust.

Root cause: In evaluateCalcString.ts, tokenize() only consumed digits and . for numbers. For 1.1368683772161603e-13mm it parsed 1.1368683772161603, stopped at e, and treated e as a unit suffix → Unknown unit: "e", which upstream is reported as an invalid calc expression.

Changes

  • Extend the number lexer in tokenize() to accept optional scientific notation (e/E, optional sign, exponent digits) before the optional unit suffix (mm, etc.)
  • Normalize -0 to 0 in the final result (avoids negative zero from unary - on dust values)

Testing

Unit tests (evaluateCalcString):

  • 1.5e-2mm0.015
  • 2E+3mm2000
  • calc(1e-13mm + 2mm)2

Integration test (<smtpad>):

  • Footprint with pcbX="-1.1368683772161603e-13mm" and pcbY="0mm" renders without source_invalid_component_property_error; pad x is ~0

Regression checks:

  • Existing calc expression tests pass (calc(board.minX + 1mm), legacy board.minx, etc.)
  • RP2040 decoupling capacitors repro passes (uses the same scientific-notation coordinate in the RP2040 footprint fixture)

to reproduce error with and without the fix:

git checkout 3477027 -- lib/utils/evaluateCalcString.ts
bun test tests/utils/evaluate-calc-string.test.ts tests/components/primitive-components/smtpad-scientific-notation-coordinates.test.tsx
git checkout HEAD -- lib/utils/evaluateCalcString.ts # restore fix

@vercel
Copy link
Copy Markdown

vercel Bot commented May 24, 2026

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

Project Deployment Actions Updated (UTC)
tscircuit-core-benchmarks Ready Ready Preview, Comment May 25, 2026 9:01am

Request Review

Copy link
Copy Markdown
Member

@imrishabh18 imrishabh18 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is the right place for the fix, can you create a test in here to demonstrate the fix

Copy link
Copy Markdown
Contributor

@seveibar seveibar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a test

@asapsav
Copy link
Copy Markdown
Author

asapsav commented May 25, 2026

added tests back

to reproduce error with and without the fix:

git checkout 34770277 -- lib/utils/evaluateCalcString.ts
bun test tests/utils/evaluate-calc-string.test.ts tests/components/primitive-components/smtpad-scientific-notation-coordinates.test.tsx
git checkout HEAD -- lib/utils/evaluateCalcString.ts   # restore fix

@imrishabh18 where is the right place to fix? new to the repo

@asapsav asapsav requested a review from imrishabh18 May 25, 2026 08:58
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.

3 participants