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 Petr Kocmid other posts by this author
Feb 11 2001 10:56AM messages near this date
Math Constants | Re: [boost] Math Constants
Hi Paul,

>  From: Paul A. Bristow [mailto: pbristow@[...].. ]
>  7 static_cast ing provides the way for users to get what they want.
>  In practice, I cannot foresee wanting more than one type at a time.
>  Either one is doing the calculations in float, double or long double,
>  but not both piFloat and piDouble at the same time. So other suggestions
>  made are not sufficiently useful here to justify their complexity.

I feel limiting to one fp representation is problematic. Consider a 3D
visualisation application, doing RT world geometry transformations in floats
(as certain API needs) and some math computation (RT signal processing) in
doubles (as another API needs) at the same time. Also, customer made numeric
types are often needed in addition, often on platforms which do not have
floating point hw at all (an extreme example: Hubble scope is controled by
286 cpu...).

I think a solution with no complexity for you would be to wrap all your
constants as statics to template class (otherwise empty, so no overhead (in
case of floats and doubles)), and define constant specialisations for them.
The class is seen by library user as a wraping namespace, not an object.

template <typename Precision>  class math_const {
public:
typedef Precision PrecisionType; // publish the type for possible reuse
static const Precision pi;
};

template<>  const float math_const<float>::pi = YOUR_PI_DEFINITION;

template<>  const double math_const<double>::pi = YOUR_PI_DEFINITION;

Usage should be quite pleasing the eyes of even the boost purists:
cout << math_const<float> ::pi << math_const<double>::pi;

Not mention the extensibility of the mechanism to custom numeric types. This
could be done with macro:
#define INSTANTIATE_PI(TYPE) template<>  const TYPE math_const<TYPE>::pi =
YOUR_PI_DEFINITION

INSTANTIATE_PI(float);
INSTANTIATE_PI(fixed<int,int> );

In later case, if fixed<int,int>  suppplies initialization from double, your
constants will work with fixed<int,int>  too.

Petr Kocmid
 pkocmid@a...
Thread:

Petr Kocmid
Jens Maurer
Ed Brey

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