Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2003-11-10 05:39:53


Hi,
    In my quest for the right operator semantics in my physical_quantity
class I came up against the need to compare
 computed f.p. values in a metafunction.

The calc looks something like this (simplified here)

    float const x_v1;
    float const x_p1,
    float const z_v1;

    float const x_v2;
    float const x_p2,
    float const z_v2;

        float const scale1 = pow(x_v1,x_p1) * z_v1;
        float const scale2 = pow(x_v2,x_p2) * z_v2;

        then the runtime equiv of the metafunction
        bool isbigger (scale1,scale2);

Obviously this cant be computed at compile time, without a lot of
complexities

however if the f.p. value x is represented by an integer constant where
log_x = log(x) * multiplier // multiplier == (say) 1e6
the calc turns into:

    static int const log_scale1 = (log_xv1 * x_p1) + log_zv1;
    static int const log_scale2 = (log_xv2 * x_p2) + log_zv1;

which of course can be computed and compared at compile time:

       static bool const is_log_bigger = log_scale1 > log_scale2;

Of course there are a lot of downsides, (most of which are soluble with more
work of course)
the f.p constants must be +ve;
The multiplier needs to be as big as possible not to lose resolution,
but small enough not to cause overflow, and of course the sizeof int makes
it platform dependent.

Therefore it needs to be used with great caution to avoid hard to spot bugs
etc.

However if there is no other way it provides a rough and ready method of
performing the required cmp.
(The limitations could probably be reduced with some "metafunction
assembler". ie ADC etc, to create bigger ints,sign etc)

Apologies if this is as old as the hills.
BTW If the technique is in use I would certainly be interested in any docs
as to its use in practise.

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