From 5fcacb80ce30ebfe9536ff36d3107284e010642d Mon Sep 17 00:00:00 2001 From: lzpel <18492524+lzpel@users.noreply.github.com> Date: Mon, 20 Jul 2026 22:25:22 +0900 Subject: [PATCH 1/2] Fix M_PI being unavailable where _USE_MATH_DEFINES comes too late #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 , 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 -> plot.cpp:1 -> openmc/plot.h:4 -> quartic_solver.cpp:1 -> -> (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 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. --- src/external/quartic_solver.cpp | 2 +- src/mesh.cpp | 13 ++++++------- src/plot.cpp | 3 +-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/external/quartic_solver.cpp b/src/external/quartic_solver.cpp index 915020ffaa3..71bdb25c3e3 100644 --- a/src/external/quartic_solver.cpp +++ b/src/external/quartic_solver.cpp @@ -1,5 +1,5 @@ -#include #define _USE_MATH_DEFINES // to make M_PI declared in Intel and MSVC compilers +#include #include #include #include diff --git a/src/mesh.cpp b/src/mesh.cpp index 89d1ff24b82..b05560d2a70 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -1,11 +1,10 @@ #include "openmc/mesh.h" #include // for copy, equal, min, min_element #include -#include // for uint64_t -#include // for memcpy -#define _USE_MATH_DEFINES // to make M_PI declared in Intel and MSVC compilers -#include // for ceil -#include // for size_t +#include // for ceil +#include // for size_t +#include // for uint64_t +#include // for memcpy #include #include // for accumulate #include @@ -1829,7 +1828,7 @@ StructuredMesh::MeshIndex CylindricalMesh::get_indices( } else { mapped_r[1] = std::atan2(r.y, r.x); if (mapped_r[1] < 0) - mapped_r[1] += 2 * M_PI; + mapped_r[1] += 2 * PI; } MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh); @@ -2123,7 +2122,7 @@ StructuredMesh::MeshIndex SphericalMesh::get_indices( mapped_r[1] = std::acos(r.z / mapped_r.x); mapped_r[2] = std::atan2(r.y, r.x); if (mapped_r[2] < 0) - mapped_r[2] += 2 * M_PI; + mapped_r[2] += 2 * PI; } MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh); diff --git a/src/plot.cpp b/src/plot.cpp index 96df1c5beb8..5c4eb946063 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -1,7 +1,6 @@ #include "openmc/plot.h" #include -#define _USE_MATH_DEFINES // to make M_PI declared in Intel and MSVC compilers #include #include #include @@ -1328,7 +1327,7 @@ std::pair RayTracePlot::get_pixel_ray( int horiz, int vert) const { // Compute field of view in radians - constexpr double DEGREE_TO_RADIAN = M_PI / 180.0; + constexpr double DEGREE_TO_RADIAN = PI / 180.0; double horiz_fov_radians = horizontal_field_of_view_ * DEGREE_TO_RADIAN; double p0 = static_cast(pixels()[0]); double p1 = static_cast(pixels()[1]); From 27619db668182a0eb1efc372d936f9d243325c28 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Tue, 21 Jul 2026 09:50:52 -0500 Subject: [PATCH 2/2] Add _USE_MATH_DEFINES in examples --- examples/custom_source/source_ring.cpp | 1 + .../parameterized_custom_source/parameterized_source_ring.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/examples/custom_source/source_ring.cpp b/examples/custom_source/source_ring.cpp index 5ab531a4a7d..bce6c27eb9e 100644 --- a/examples/custom_source/source_ring.cpp +++ b/examples/custom_source/source_ring.cpp @@ -1,3 +1,4 @@ +#define _USE_MATH_DEFINES #include // for M_PI #include // for unique_ptr diff --git a/examples/parameterized_custom_source/parameterized_source_ring.cpp b/examples/parameterized_custom_source/parameterized_source_ring.cpp index 9756e1b9774..3aabf358180 100644 --- a/examples/parameterized_custom_source/parameterized_source_ring.cpp +++ b/examples/parameterized_custom_source/parameterized_source_ring.cpp @@ -1,3 +1,4 @@ +#define _USE_MATH_DEFINES #include #include #include