Skip to content

Fix M_PI being unavailable where _USE_MATH_DEFINES comes too late#4023

Merged
paulromano merged 2 commits into
openmc-dev:developfrom
lzpel:pr1-alt-use-pi
Jul 21, 2026
Merged

Fix M_PI being unavailable where _USE_MATH_DEFINES comes too late#4023
paulromano merged 2 commits into
openmc-dev:developfrom
lzpel:pr1-alt-use-pi

Conversation

@lzpel

@lzpel lzpel commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes M_PI being undeclared in three files ( Resolve #3175 ). #3238 added
#define _USE_MATH_DEFINES to them, but in all three it sits below an include that has
already pulled in <cmath>, so math.h is guarded by then and the definition never takes
effect. quartic_solver.cpp differs between the two because MSVC's <algorithm> reaches
math.h transitively and MinGW's libstdc++ does not. Since which header gets there first is
implementation-defined, the only position that is safe everywhere is above every include —
so mesh.cpp and plot.cpp sidestep the macro entirely. No test is added: PI is
bit-identical to M_PI, so there is no behaviour change to assert.

file MSVC 19.51 MinGW GCC 14.2 change
src/mesh.cpp 2 errors 2 errors use PI from include/openmc/constants.h
src/plot.cpp 1 error 1 error use PI from include/openmc/constants.h
src/external/quartic_solver.cpp 3 errors ok vendored, namespace oqs: keep M_PI, move #define above the includes

All three compile successfully with no external -D_USE_MATH_DEFINES after this fix.

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 18) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

openmc-dev#3238 added _USE_MATH_DEFINES to mesh.cpp, plot.cpp and quartic_solver.cpp so
that M_PI would be declared on Intel and MSVC. In all three the definition sits
below an include that has already pulled in <cmath>, and once math.h has been
processed its include guard is set, so the definition never takes effect.

  mesh.cpp:1  -> openmc/mesh.h -> openmc/position.h:4 -> <cmath>
  plot.cpp:1  -> openmc/plot.h:4                      -> <cmath>
  quartic_solver.cpp:1 -> <algorithm>                 -> <cmath>  (MSVC)

Measured with MSVC 19.51.36248 (VS 2026) and MinGW-w64 GCC 14.2.0, compiling
each file with no external -D_USE_MATH_DEFINES:

                       MSVC before  MSVC after  MinGW before  MinGW after
  mesh.cpp             2 errors     ok          2 errors      ok
  plot.cpp             1 error      ok          1 error       ok
  quartic_solver.cpp   3 errors     ok          ok            ok

quartic_solver.cpp is where the two toolchains differ: MSVC's <algorithm>
reaches math.h transitively, libstdc++'s does not. Which header first reaches
math.h is implementation-defined, so the only position that is safe everywhere
is above every include.

mesh.cpp and plot.cpp therefore drop the macro entirely and use the PI constant
already in constants.h, which both files already include and which is
bit-identical to M_PI. quartic_solver.cpp is vendored third-party code in
namespace oqs, so it keeps M_PI and just moves the definition to the top.

In mesh.cpp the definition was also splitting the include block in two, which
let the two halves stay individually sorted; removing it merges them and
clang-format sorts the result.
@lzpel
lzpel requested a review from pshriwise as a code owner July 20, 2026 14:21

@paulromano paulromano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix @lzpel!

@paulromano
paulromano enabled auto-merge (squash) July 21, 2026 14:54
@paulromano
paulromano merged commit 852f927 into openmc-dev:develop Jul 21, 2026
16 checks passed
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.

error: use of undeclared identifier 'M_PI'

2 participants