Boost logo

Boost :

Subject: Re: [boost] Numeric constants
From: John Maddock (john_at_[hidden])
Date: 2009-01-31 05:22:38


> I played around with this type of idea in the geometry code I've been
> working on. In my case I wanted to support any kind of type as well as
> rational types (for which even a 100 decimal pi might be considered poor
> resolution?). I've never really worked in a domain requiring this high of
> a
> precision for pi.. but figured it was fun to give it a whirl.
>
> One use case for such a thing would be if you are using integral types
> for defining the geometry but still need to work with some constants like
> PI (in very high precisions).
>
> So for an example I had a specialization say for an arbitrary big int type
> (with a traits specialization to limit the precision to something ....
> mortal):
>
> template <>
> struct constants<my_big_int>
> {
> typedef my_big_int int_type;
> typedef rational_promotion_traits< int_type >::rational_type
> rational_type;
>
> //! \brief Calculate the required digits of pi for type my_big_int.
> static inline rational_type pi()
> {
> //! Calculate PI to the required precision using a spigot algorithm.
> static rational_type _pi = detail::calculate_pi< rational_type,
> numeric_traits< int_type >::precision
> >::pi();
> return _pi;
> }
> };
>
> And voila, I had a means to use a tailor fit approximation of PI with my
> integral geometry types. I always figured this really didn't belong to the
> geometry code.. and that it should be factored out and into a more general
> numeric library (but there wasn't one that I saw at the time). Would
> Boost.Math already have a facility covering this use case?

Sort of, you would specialize boost::math::constants::pi:

namespace boost{ namespace math{ namespace constants{

template<>
inline rational_type pi<rational_type>()
  {
      //! Calculate PI to the required precision using a spigot algorithm.
      static rational_type _pi = detail::calculate_pi< rational_type,
                                numeric_traits< int_type >::precision
>::pi();
      return _pi;
  }
};

}}} // namespaces

And then you can write generic code involving PI that functions the same for
your rational type as it does for say double or float.

Is that what you were looking for?

John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk