Skip to content

always define M_PI#34

Closed
Karsten1987 wants to merge 3 commits intoros:masterfrom
ros2:math_h
Closed

always define M_PI#34
Karsten1987 wants to merge 3 commits intoros:masterfrom
ros2:math_h

Conversation

@Karsten1987
Copy link
Copy Markdown

I changed <cmath> to <math.h> in order to consistently activate the _USE_MATH_DEFINES symbols such as M_PI when compiling a complete windows stack workspace, such as ROS2.0

The reason for opening this PR is that when building the ROS2.0 workspace on Windows, this package fails with M_PI symbols not being defined. The problem is that cmath different from math.h does only once check whether the math symbols are activated. If something higher in the pipeline overwrites these definitions, cmath gets built wo/ these symbols.

see this stackoverflow thread for more information.

@traversaro
Copy link
Copy Markdown
Contributor

math.h and cmath are not completely equivalent, in particular math.h do not properly define all the type-safe variants of the sin, cos function in the std:: namespace.

Even if this features are not used in urdfdom_headers (all the code here uses double variables) I would be a bit afraid to silently change this, that could cause tricky changes in downstream users of urdfdom_headers.

Back in 8fdf48f I added the definition of the _USE_MATH_DEFINES macro, but in retrospective I think is a poor solution, exactly for this kind of problems.

Some alternative solutions could be:

  • define and export the _USE_MATH_DEFINES macro at the CMake level,
  • avoid to use the M_PI macro at all.

Interesting discussions on this:

@Karsten1987
Copy link
Copy Markdown
Author

we currently use the version of this PR within our ROS2 base, and it's working fine. Question here is what do you with this PR.

@traversaro
Copy link
Copy Markdown
Contributor

There are several possible options, including this PR itself.

In my opinion the best solution in the short term is to transform the <project>-config.cmake to expose this library as a CMake INTERFACE import library (see Eigen for a good example of this: https://bitbucket.org/eigen/eigen/src/a038e46f2d3e40b6da3af67822d647335ff0fc44/cmake/Eigen3Config.cmake.in?at=default&fileviewer=file-view-default ) and add their (with target_compile_definitions(<target> PUBLIC ...) the _USE_MATH_DEFINES flag. Clearly this solution involves a bit of work both at urdfdom level and in the libraries consuming urdfdom, so it is up to the maintainers decide if it make sense to go for it or not.

In the long term it would make sense to just stop using non-standard definitions (M_PI) in public headers.

@scpeters
Copy link
Copy Markdown
Contributor

M_PI is only used in two places in a single function, so I'd be inclined just to hard-code it there. That risks breaking downstream code that was implicitly including math.h, but that seems easier, since it's just a constant used in one function.

@Karsten1987
Copy link
Copy Markdown
Author

I am fine with explicitly defining M_PI here. What's the recommended procedure to get this PR done?

@scpeters
Copy link
Copy Markdown
Contributor

I think we can revert to using cmath and replace the instances M_PI with a const double

@Karsten1987 Karsten1987 changed the title include math.h always define M_PI May 3, 2017
@Karsten1987
Copy link
Copy Markdown
Author

I just reverted my initial commit and defined M_PI as a const double.

@scpeters
Copy link
Copy Markdown
Contributor

Here's an approach I would consider taking:

@scpeters
Copy link
Copy Markdown
Contributor

scpeters commented Aug 3, 2017

See #38 for an alternative approach.

@sloretz
Copy link
Copy Markdown
Contributor

sloretz commented Oct 26, 2017

I'll close this in favor of #38

@sloretz sloretz closed this Oct 26, 2017
@Karsten1987 Karsten1987 deleted the math_h branch January 15, 2020 15:35
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.

5 participants