Boost logo

Boost :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2005-03-16 04:35:32

Andy Little writes:
> The problem: For some Integral Constants values mpl::divides<A.,B>
> truncates.

Yep. Note that it's no different than

    int divides( int A, int B )
        return A / B;

> The solution: A new operator... mpl::rational_divides<A,B> When it is
> desired not to truncate, this form will preserve the fractional part
> by if necessary converting to a rational number. With this operator in
> place it is also possible to 'reduce' rationals to integers where
> possible and further to keep integral constants as integral types by
> default, knowing that conversion will be automatic if a fraction is
> required to prevent loss of precision.

Could you provide a specific use case where the above would be preferable
over something like

    mpl::divide< to_rational<A>,B >


> No mpl::divides<A.,B> is available if A or B is a rational types,
> because it is assumed that if you are using rationals you require the
> fraction preserving behaviour.

I'd assume that 'divides' on rational types actually wouldn't truncate
anything. Why one would prefer to have a separate metafunction for
doing the right thing instead of simply overriding the standard one?

> A numeric_cast to an integral type can be used to solve this where
> necessary.
> Errors of using divides<A,B> on rationals will show at compile time.
> Thanks to Alexey Gurtovoy for the conversion mechanism

You're welcome!

Aleksey Gurtovoy
MetaCommunications Engineering

Boost list run by bdawes at, gregod at, cpdaniel at, john at