Skip to content

gh-70450: Fix MSVC /fp:strict builds - #154849

Open
yhay81 wants to merge 2 commits into
python:mainfrom
yhay81:codex/gh-70450-fp-strict
Open

gh-70450: Fix MSVC /fp:strict builds#154849
yhay81 wants to merge 2 commits into
python:mainfrom
yhay81:codex/gh-70450-fp-strict

Conversation

@yhay81

@yhay81 yhay81 commented Jul 29, 2026

Copy link
Copy Markdown

Fixes #70450.

Summary

  • replace floating-point arithmetic in static initializers with equivalent binary64 literals
  • keep the cmath special-value tables read-only and decode their finite NaN sentinel when a value is read
  • create the exported math and cmath NaN constants with nan(""), avoiding floating-point exceptions during strict-mode imports
  • document the precomputed literals and add exact-value regression tests
  • add a build NEWS entry

Root cause

MSVC's strict floating-point mode does not accept the affected arithmetic expressions as static initializers. In C mode, the UCRT NAN expansion also performs runtime floating-point arithmetic under /fp:strict; using it during module initialization set FE_INVALID | FE_OVERFLOW | FE_INEXACT.

The cmath tables therefore retain a finite sentinel in static storage and convert only the selected result to a quiet NaN with nan(""). This keeps the tables const, avoids process-global initialization state, and preserves two-dimensional array bounds.

Validation

  • built PCbuild/pythoncore.vcxproj in Debug x64 with ICCBuild=true (selecting FloatingPointModel=Strict): passed
  • built the same project with the normal floating-point configuration: passed
  • cleared the UCRT floating-point status in fresh processes, then imported math and cmath: both remained 0x0, with errno 0
  • ran python_d.exe -m test --timeout 300 -j1 test_math test_cmath test_float test_strtod: 186 tests passed, 4 skipped
  • ran Tools/c-analyzer/check-c-globals.py --format summary --traceback Modules/cmathmodule.c with the GCC preprocessor: passed
  • ran pre-commit on all six changed files, Tools/patchcheck/patchcheck.py, and git diff --check: passed

AI assistance: This patch was developed with assistance from OpenAI Codex; I reviewed and tested the final changes.

Replace floating-point expressions that MSVC rejects in static initializers under strict semantics. Initialize cmath NaN table entries once because NAN is not a constant expression in MSVC C mode.
@python-cla-bot

python-cla-bot Bot commented Jul 29, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@yhay81
yhay81 marked this pull request as ready for review July 29, 2026 05:51
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.

Cannot compile with /fp:strict with MSVC

1 participant