ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> boost
boost
Re: [boost] Math Constants
by Ed Brey other posts by this author
Feb 9 2001 3:00PM messages near this date
Re: [boost] Math Constants | Boost python library and Numpy
>  As a result I am starting again with a much simpler proposal -
>  a file of long double #defined constants
> 
>  #define SQRT2 1.41421356237309504880168872420969807857L
>  // and lots more
> 
>  and some recommendations for C++ use:
> 
>  const float sqrt_2 = static_cast<float>SQRT2;

The macro seems like a good, pragmatic way of minimizing loss of precision
within a compiler. To take this a step further, perhaps there is a way to
eliminate the macros altogether with the help of templates. Consider this
having the generator output a header like this:

namespace boost { namespace constant {
template<typename T>  inline T sqrt_2() {
return static_cast<T> (1.41421356237309504880168872420969807857L);
}
}}

The user code would then look more like this:

int main() {
using boost::constant;
std::cout << constant::sqrt_2<float> () << '\n';
}

Of course, assigning a local variable to take on the constant value is
fine, too. The templatized format reminds me of numeric_limits, only with
a templatized function instead of a class. A class could be used as well,
allowing for usage like constant::sqrt_2<float> ::value, but I can't think
of where there would be an advantage here. For integers, having a
constant instead of a function is useful, but for floating types I don't
know of such an advantage.
Thread:

Petr Kocmid
Jens Maurer
Ed Brey

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved