// defConst.h // Define constants C preprocessor style. #define _PI 3.1415926535897932384626433832795028841972L #define _E 2.7182818284590452353602874713526624977572L // mathConstants.hpp #include "defConst.h" // C Preprocessor version of constants. #define _PI ... // C++ versions of constants. // If class T is float, // warning C4305: 'return' : truncation from 'const long double' to 'float' // so disable warning thus: #pragma warning(disable : 4305) template struct math_constants { static T pi () {return _PI;} static T e () {return _E} }; #include "undefConst.h" // #undef _PI // To avoid namespace pollution. // constants.hpp #include "defConst.h" // C Preprocessor version of constants. #define _PI ... // C++ const long versions of constants. const long double pi = _PI; // pi const long double e = _E; // e #include "undefConst.h" // #undef _PI // To avoid namespace pollution. // undefConst.h // Undefine all constants to avoid namespace pollution. #undef _PI #undef _E // testdefineConstants.cpp Math Constants Demo program for all uses. #include #include #include #include "mathConstants.hpp"// for math_constants::pi(); #include "constants.hpp" // For const long double pi; extern const long double pi ; // pi extern const long double e; // e //#include "defConst.h" // Only if want to use _PI directly. // For example: const float pi = _PI; // And remember that constants.hpp and mathConstants.hpp both #undef all constants. using std::cout; using std::cin; using std::endl; using std::dec; using std::hex; using std::setprecision; int main() { cout << "Test " << __FILE__ << ' ' << __TIMESTAMP__ << endl; // Example of math constant using template function. double myPi = math_constants::pi(); cout << "myPi as double (default precision 6 decimal digits) is " << myPi << endl; // Ensure all significant digits and two noisy digits are displayed. cout << setprecision(std::numeric_limits::digits10 + 2); cout << "pi as double (all significant digits & two noisy) is " << myPi << endl; cout << setprecision(std::numeric_limits::digits10 + 2); cout << "pi as float (all significant digits & two noisy) is " << math_constants::pi() << endl; cout << setprecision(std::numeric_limits::digits10 + 2); cout << "pi as long double (all significant digits & two noisy) is " << math_constants::pi() << endl; // Check that #defined constants have been undefined, // unless #include "defConst.h" for the preprocessor constants _PI etc // Note need to #include "defConst.h" AFTER other constants include files. #ifdef _PI cout << "_PI is still defined!" << endl; cout << "_PI = " << _PI << endl; #endif #ifndef _PI cout << "_PI is NOT defined." << endl; #endif // Using naive const long double constants, #include "constants.hpp" cout << setprecision(std::numeric_limits::digits10 + 2); cout << "extern pi as const long double is " << pi << endl; return 0; } // main /* Output is: Test J:\Cpp\constants\defineConstants\testDefineConstants.cpp Wed Mar 28 11:24:29 2001 myPi as double (default precision 6 decimal digits) is 3.14159 pi as double (all significant digits & two noisy) is 3.1415926535897931 pi as float (all significant digits & two noisy) is 3.1415927 pi as long double (all significant digits & two noisy) is 3.1415926535897931 _PI is NOT defined. extern pi as const long double is 3.1415926535897931 Press any key to continue */