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 <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.
This commit is contained in:
lzpel 2026-07-20 22:25:22 +09:00
parent 05d01274a7
commit 5fcacb80ce
3 changed files with 8 additions and 10 deletions

View file

@ -1,5 +1,5 @@
#include <algorithm>
#define _USE_MATH_DEFINES // to make M_PI declared in Intel and MSVC compilers
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>

View file

@ -1,11 +1,10 @@
#include "openmc/mesh.h"
#include <algorithm> // for copy, equal, min, min_element
#include <cassert>
#include <cstdint> // for uint64_t
#include <cstring> // for memcpy
#define _USE_MATH_DEFINES // to make M_PI declared in Intel and MSVC compilers
#include <cmath> // for ceil
#include <cstddef> // for size_t
#include <cmath> // for ceil
#include <cstddef> // for size_t
#include <cstdint> // for uint64_t
#include <cstring> // for memcpy
#include <limits>
#include <numeric> // for accumulate
#include <string>
@ -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);

View file

@ -1,7 +1,6 @@
#include "openmc/plot.h"
#include <algorithm>
#define _USE_MATH_DEFINES // to make M_PI declared in Intel and MSVC compilers
#include <cmath>
#include <cstdio>
#include <fstream>
@ -1328,7 +1327,7 @@ std::pair<Position, Direction> 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<double>(pixels()[0]);
double p1 = static_cast<double>(pixels()[1]);