Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

anthrotype
Copy link
Member

@anthrotype anthrotype commented Aug 21, 2025

@cmyr noticed that sometimes with some degenerate cubic curves (e.g. those where control points are on the same line), cu2qu can produce different number of quadratic off-curves depending on whether it is running in compiled cython mode (as one would get when installing from the wheels that we publish to PyPI) vs pure-python mode (e.g. when pip installing fonttools in editable from a local git checkout inside a python environment that doesn't have cython, or without explicitly asking for FONTTOOLS_WITH_CYTHON=1 at built time).

I isolated one of these offending curves (from BilboPro.glyphs) to create the test.

The calc_intersect function calls dot to compute the vector dot product, in this situation this is expected to be 0.0 and trigger a ZeroDivisionError.

But for some reasons (implementation details of floating-point and complex math in CPython vs C/Cython) we don't get 0.0 in both modes, but something very close (about 2e-17) to 0.0 in one of the two, whereas the other one gives exactly 0.0. They are both correct within IEEE 754 floating-point precision, but only an exact 0.0 triggers the ZeroDivisionError.

To be safe and get consistent results in either case without relying on particular implementation details, we should just clamp to 0.0 when |result|<1e-15 and go home.

the offending curve was taken from 'brevecomb_gravecomb.case' glyph of BilboPro.glyphs
@anthrotype
Copy link
Member Author

the test unexpectedly passed on the CI, but it actually fails on my local machine (python3.13 on macOS, from homebrew).
The CI is only running 3.13 on linux, not on macos. I'll see if I can reproduce on the CI as well, but this is yet another argument not to rely on brittle floating-point math.

@anthrotype
Copy link
Member Author

@behdad
Copy link
Member

behdad commented Aug 21, 2025

LG

@anthrotype anthrotype merged commit a7d122f into main Aug 21, 2025
17 of 19 checks passed
@anthrotype anthrotype deleted the fix-cython-zerodiv branch August 21, 2025 17:39
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.

2 participants