Are numeric constant makros like
M_PIknown from the C-librarymath.hpart of the C++ standard?
I cannot find them in my reference.What is the best way to define custom constants?
Is aconstants.hppwithstatic constexpr int foo = 7;with a special namespace a good solution?If the makros from question 1 do exist, should I prefer them for readability or define my own constants (like in 2 or in a better way) for type safety?
Asked
Active
Viewed 1,578 times
1
Baum mit Augen
- 47,658
- 24
- 139
- 177
-
1http://stackoverflow.com/q/1727881/1147772 Might help – Drax Apr 29 '14 at 15:10
3 Answers
3
You could use boost -
#include <boost/math/constants/constants.hpp>
using namespace boost::math::constants;
double circumference(double radius)
{
return radius * 2 * pi<double>();
}
see the documentation
benf
- 835
- 1
- 8
- 27
2
Neither the C Standard nor the C++ Standard defines constant M_PI.
There is no sense to use keyword static in a constant definition because by default constants have internal linkage.
Before defining a constant you should look through the POSIX standard.
niklasfi
- 14,065
- 7
- 37
- 52
Vlad from Moscow
- 265,791
- 20
- 170
- 303
0
M_PIis not standard.I think this is more of a preference. I've seen the following methods:
#define PI 3.1415 const double PI = 3.1415; const double kPi = 3.1415;Again, I think its a preference or it could depend on what you are actually doing. Also try this:
#define _USE_MATH_DEFINES #include <math.h>
csnate
- 1,459
- 4
- 15
- 30