Boost logo

Boost :

From: Peder Holt (peder.holt_at_[hidden])
Date: 2005-06-08 11:11:25


On 6/8/05, David Abrahams <dave_at_[hidden]> wrote:
> Peder Holt <peder.holt_at_[hidden]> writes:
>
> > Is there any interest in a compile-time meta-version of double?
> >
> > Syntax:
> >
> > //Definition
> > typedef META_DOUBLE(3.1415926535897932384626433) pi;
> > //Or
> > META_DOUBLE(3.1415926535897932384626433) pi_;
>
> Whoa, that's nice syntax!

Thanks :)

>
> I'm still trying to guess how you do it. Does the PP treat 3.14 as
> three tokens?

Currently, I use a naive approach, that does not handle rounding
errors correctly for exponents.

It goes something like this:

#define REMOVE_EXP256(number) number*(number>1e256?1e-256:1.)
#define REMOVE_EXP128(number) \
REMOVE_EXP256(number)*(REMOVE_EXP256(number)>1e128?1e-128:1.)
etc etc
to
#define REMOVE_EXP1(number)\
REMOVE_EXP2(number)*(REMOVE_EXP2(number)>1e1?1e-1:1.)

And similar for negative exponents.

After removing the exponent (normalising the number)
I convert the number to int:

#define BOOST_META_NORMALISED_DOUBLE_TO_INTA(value)
(value<0?-1:1)*int((value)*1e8)

#define BOOST_META_NORMALISED_DOUBLE_TO_INTB(value)
(value<0?-1:1)*int(((value)*1e8-int((value)*1e8))*1e8)

As mentioned, since double is 2-based, the above approach gives
round-off errors since it is 10-based.

Replacing the above with a 2-based exponent seems to solve this problem

>
> > //Mathematical operations
> > typedef math::meta::add<pi,pi>::type pi2;
> >
> > //Evaluation:
> > assert(META_DOUBLE_EVAL(pi2)==3.1415926535897932384626433*2);
>
> Really, even with FP rounding errors? I guess multiplying by 2 is
> fairly safe...

I think so, with the 2-based exponent I currently use.

>
> > This implementation uses two ints to represent the decimals, and a
> > short to represent the exponent.
>
> I don't understand why you'd choose int for one and short for the
> other when they have the same range requirements: both int and short
> are required to be at least 16 bits and neither short nor int is
> required to be more than 16 bits. Can you explain that?

Should have used long in stead of int. This has been fixed.
Correction from above:
I use 64 bits to represent the mantissa + sign
and 16 bits to represent the exponent

Regards, Peder

>
> --
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>


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