Math constants for C++

C and C++ standards have never specified any math constants. However, most old versions of C and C++ standard libraries and compilers used to provide many math constants through their math.h and cmath header files. However, in recent years many of these compilers and standard libraries have removed these constants from their header files.

This results in code which uses such constants, say M_PI in throwing errors like this:

C:\Foobar.cpp(42): error C2065: 'M_PI' : undeclared identifier

I found that GCC still provides these constants, while Visual Studio 2012 provides this only if _USE_MATH_DEFINES is defined. The latest Visual Studio 2015 has completely removed these constants from their header files.

If you are faced with a situation where these constants are not available, it is easy to redefine them in your own code.

Suggested values of these math constants can be found here. Here is the same obtained from math.h of Visual Studio 2012:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s