Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2005-05-10 14:10:04


"Daniel Frey" <d.frey_at_[hidden]> wrote
> Andy Little wrote:
>>
>> "Tobias Schwinger" <tschwinger_at_[hidden]> wrote
>>
>>> Btw. Daniel Frey's library looks very interesting - did you take a
>>> look at it
>>> ?
>>
>> I looked at it during the math constants review. I didnt look at the latest
>> version, but I have now. Unfortunately it doesnt compile on VC7.1 but
>> looks like
>> a smarter version of my offering. (I guess that 's where the idea for
>> mine came
>> from.) OTOH it doesnt do eg double(pi) which syntax I happen to like.
>
> Me too, but it doesn't work for generic types T. If T has several ctors
> that take different types (e.g. float, double, T), pi has no way to
> decide which cast would be appropriate.

I got around this ( I thought) in my quickie by using a traits class, but its
messy... I guess.
However IMO it shouldnt be convertible to anything. There needs to be some
concept that it models. FWIW I would be content with , float, double long
double. IOW numeric types. But ... eg FWIW my version still fails in
boost::numeric::interval. This is always going to be the problem when
trying to make one type maquerade as another :-(

>>> It uses expression templates to allow "constant expressions" e.g: 'pi
>>> * two'
>>> or 'pow(two,one / twelve)'
>
> Hm... I just had the idea of allowing a simple 'constant_<2> two;' to
> declare integer constants, but immediately ran into a more subtle
> problem involving two_value. Seems there's still something to redesign,
> so I have to play with this idea for some time...

A 'math constant' is expected to be pretty unexciting, which this doesnt
deliver. Maybe if it was repackaged as say a quirky 'smart constant', rather
than The std::math_constant it would be more gratefully received.

IOW The big hurdle is that its slightly weird and dont always act like the type
its supposed to replace. ( Which is why I am pretty content with my original
solution.eg template <typename T> struct constant{ T const & pi; T const &
e;... etc}Ok.... its not extensible (re higher in the thread) but it is simple
and acts
exactly as you would expect a T to always, (because it is a T);)
The alternative is just a function eg pi<T>(); I guess.

>> Thats interesting . I need this type of thing , but its clumsy in use.
>
> What is clumsy, what would be an improvement? Or a better syntax? Even
> if it seems impossible, I would like to know what others consider
> intuitive. If you have an idea how it should look like, please share it.
>
>> In an ideal world I guess one would be able to say e.g pow(my_num, n
>> ,d );
>> and get the function template <typename T, int N, int D > T pow(T,N, D);
>
> Here, I'm at a loss. Can you elaborate, please?

sure .. .... but of course it isnt doable in the language.
(BTW the pi * pi stuff is cool !. Sorry... it appears I have been
dismissive of that.)

 int val = my::pow(9,3,2); eg replaces std::pow( static_cast<double>(9)
,3./2) with the above signature.
    ( val --> 27 )
today you might do this as my::pow<9,3,2>();

Another example of using compile time constants directly in expressions

point + point --> affine_point_combination<2>
point + point + point --> affine_point_combination<3> etc..
point p = affine_combination<N>() / n ; //( where n == N and the exact
function depends on whether n is a compile time constant and the runtime version
throws an exception if n !=N )
affine_combination<N> / x ( where x is a compile time evaluable constant And
x!=N) ---> Compile time Error

I guess however, that this would require figuring out the type of constness of
n.

regards
Andy Little


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