Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2003-06-10 10:21:49


Daniel Frey wrote:

>namespace math
>{
> // Generic base class for all constants
> template< typename T, template< class > class F > struct constant
> {
> [snip]
> };

I like this idea, especially how it allows the constant to be transparent
within an expression.

> // And their relationship:
> // The user can now write 'pi / two' and needn't remember
> // whether it was 'half_pi' or 'pi_by_2' or whatever...
> half_pi_t operator/( const pi_t&, const two_t& ) { return half_pi_t();
>}

> minus_one_t operator-( const one_t& ) { return minus_one_t(); }
> one_t operator-( const minus_one_t& ) { return one_t(); }
> one_t sin( const half_pi_t& ) { return one_t(); }

This is a very powerful concept, allowing the compiler to simplify certain
expressions.

> long double r = 0;
> for( int i = 0; i < 10000000; ++i )
> r = d * -sin( pi / 2. ); // try replacing the "2." by "two" - makes
>this test program 100x faster!

This is strange, but shows compiler optimization in action:

r = d * -sin( pi_t / two_t )
==> r = d * -sin( half_pi_t )
==> r = d * -one_t
==> r = d * minus_one_t

This could be further optimized by adding the rules

   T operator*( T val, one_t ){ return( val ); }
   T operator*( T val, minus_one_t ){ return( -val ); }
   T operator+( T val, zero_t ){ return( val ); }

-----

Some general comments:

[1] I agree that custom types should be supported in the constant framework.

[2] The 40 decimal places should be used for irrational numbers (i.e. most
mathematical constants like pi, e, sqrt(2)).

[3] Physics-based constants should be definable within the constants
framework and should be set to their current accuracy (possibly making use
of intervals).

[4] Other types beyond real numbers should be supportable within the
constant framework (intervals, matrices, complex numbers, etc), e.g.
   matrix2x2 identity( 1, 0, 0, 1 );

Regards,
Reece

_________________________________________________________________
Sign-up for a FREE BT Broadband connection today!
http://www.msn.co.uk/specials/btbroadband


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