// test_circle_area.cpp // (C) Copyright Paul A Bristow, hetp Chromatography, 2003 // Permission to copy, use, modify, sell and distribute this software // is granted provided this copyright notice appears in all copies. // This software is provided "as is" with express or implied warranty, // and with no claim as to its suitability for any purpose. #include #include // for numeric_limits #include "function_constants.hpp" using std::cout; using std::endl; const char nl = '\n'; const char tab = '\t'; const char space = ' '; // Question posed by Joerg Walter, 22 Jan 2003 22:14 template T circle_area(const T& radius) { // Usage example: circle_area( 2.) // or circle_area(2.F) return boost::math::constant< T, boost::math::pi_tag >() * radius * radius; } int main() { cout << "Test " << __FILE__ << ' ' << __TIMESTAMP__ << endl; // Calculate number of significant digits, strictly // ceil(1 + std::numeric_limits::digits*log10(2.)) // but need an approximation that does not use floating-point // to compute correctly at compile time. int float_significand_digits = std::numeric_limits::digits; // 24 bits int float_significant_digits = 2 + float_significand_digits * 30101/100000; int double_significand_digits = std::numeric_limits::digits; // 53 bits int double_significant_digits = 2 + double_significand_digits * 30101/100000; // 53 bits for MSVC cout.precision(double_significant_digits);// Ensure they are all output. // This is needed to show the difference between pi float and double // (and long double if not MSVC). // Or for using float and double constants at the same time, // users can explicitly access fully specified constants at any time: cout.precision(float_significant_digits); // Ensure all float significant (9) digits are displayed. cout << "float pi = " << boost::math::float_constants::pi << "\n"; // 3.14159274 cout.precision(double_significant_digits); cout << "double pi = " << boost::math::double_constants::pi << "\n"; // 3.1415926535897931 cout << "float pi = " << boost::math::float_constants::pi << "\n"; // double_sig_digits 3.1415927410125732 cout << "circle_area(1.) = " << circle_area(1.) << endl; // Explicit type float. cout << "circle_area(1.) = " << circle_area(1.) << endl; // Explicit type cout << "circle_area(1.F) = " << circle_area(1.F) << endl; // Implicit type float. cout << "circle_area(1.) = " << circle_area(1.) << endl; // Implicit type double. cout << "circle_area(1.L) = " << circle_area(1.L) << endl; // implicit type long double. cout << "circle_area(1.F) = " << circle_area(1.F) << endl; // Explicit over-rides implicit. cout << "circle_area(1.) = " << circle_area(1.)<< endl; // same as double for MSVC. // cout << "circle_area(1) = " << circle_area(1)<< endl; // Implicit int - does not link! // cout << "circle_area(1.) = " << circle_area(1.)<< endl; // Explicit int - does not compile! cout << "circle_area(2.) = " << circle_area(2.) << endl; // Explicit type float. cout << "circle_area(2.) = " << circle_area(2.) << endl; // Explicit type cout << "circle_area(2.F) = " << circle_area(2.F) << endl; // Implicit type float. cout << "circle_area(2.) = " << circle_area(2.) << endl; // Implicit type double. cout << "circle_area(2.L) = " << circle_area(2.L) << endl; // implicit type long double. cout << "circle_area(2.F) = " << circle_area(2.F) << endl; // Explicit over-rides implicit. float pi_f = boost::math::constant< float, boost::math::pi_tag >(); cout << "boost::math::constant< float, boost::math::pi_tag >() " << pi_f << endl; return 0; } // int main() /* Output is Test test_circle_area.cpp Thu Jan 23 00:06:28 2003 float pi = 3.14159274 double pi = 3.1415926535897931 float pi = 3.1415927410125732 circle_area(1.) = 3.1415927410125732 circle_area(1.) = 3.1415926535897931 circle_area(1.F) = 3.1415927410125732 circle_area(1.) = 3.1415926535897931 circle_area(1.L) = 3.1415926535897931 circle_area(1.F) = 3.1415926535897931 circle_area(1.) = 3.1415926535897931 circle_area(2.) = 12.566370964050293 circle_area(2.) = 12.566370614359172 circle_area(2.F) = 12.566370964050293 circle_area(2.) = 12.566370614359172 circle_area(2.L) = 12.566370614359172 circle_area(2.F) = 12.566370614359172 boost::math::constant< float, boost::math::pi_tag >() 3.1415927410125732 Press any key to continue */