Boost logo

Boost :

From: Guillaume Melquiond (guillaume.melquiond_at_[hidden])
Date: 2004-03-01 05:25:53


Le lun 01/03/2004 à 01:06, Thorsten Ottosen a écrit :

> namespace std
> {
>
> #define CNAME( Var ) Var##_constant_t
>
> #define OVERLOAD_CONTEXT( ConstType, FP, FPV ) \
> inline FP operator+( FP l, ConstType ) \
> { return l + FPV; } \
> inline FP operator+( ConstType, FP r ) \
> { return r + FPV; } \
> inline FP operator-( FP l, ConstType ) \
> { return l - FPV; } \
> inline FP operator-( ConstType, FP r ) \
> { return FPV - r; } \
> inline FP operator*( FP l, ConstType ) \
> { return l * FPV; } \
> inline FP operator*( ConstType, FP r ) \
> { return r * FPV; } \
> inline FP operator/( FP l, ConstType ) \
> { return l / FPV; } \
> inline FP operator/( ConstType, FP r ) \
> { return FPV / r; } \
>
> #define DEFINE_FP_CONSTANT( Name, FV, DV, LDV ) \
> struct CNAME( Name ) \
> { \
> CNAME( Name ) () {} \
> operator float() const { return FV; } \
> operator double() const { return DV; } \
> operator long double() const { return LDV; } \
> }; \
> OVERLOAD_CONTEXT( CNAME( Name ), float, FV ) \
> OVERLOAD_CONTEXT( CNAME( Name ), double, DV ) \
> OVERLOAD_CONTEXT( CNAME( Name ), long double, LDV ) \
> const CNAME( Name ) Name;
>
> DEFINE_FP_CONSTANT( pi, 3.14F, 3.14232, 3.1423211223L )
>
> } // namespace std

Unless I'm missing something, it seems to me that the constants can only
be defined for built-in floating-point numbers. Indeed, once the
structure for a pi constant has been defined by DEFINE_FP_CONSTANT (in
the math_constant.hpp header), the user or others libraries cannot add
new values for their own numerical types.

With respect to that, I think the proposal of Daniel Frey is a lot more
interesting since it allows user-defined types and provides automatic
type deduction so that complex constants do not have to have their own
names. Being able to write "interval<double> a = 2 * pi;" and being sure
it is mathematically correct and accurate is great.

Regards,

Guillaume


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