Boost logo

Boost :

From: Paul A Bristow (pbristow_at_[hidden])
Date: 2007-05-15 12:09:23


-----Original Message-----
>From: boost-bounces_at_[hidden]
>[mailto:boost-bounces_at_[hidden]] On Behalf Of Matthias Schabel
>Sent: 02 May 2007 19:30
>To: boost_at_[hidden]; boost-announce; boost-users
>Subject: [boost] [math toolkit] Review results

>2) Jeff Garland also pointed out, rightly in my opinion, that
>attempts to use statistical functions that do not exist for a
>distribution should fail to compile rather than leading to a runtime
>error (e.g. mean of a Cauchy distribution). A reasonable method of
>implementing this should be devised or a strong argument for why it
>is not feasible/desirable provided.

I have begun looking at this.

The current code is:

template <class RealType>
inline RealType mean(const cauchy_distribution<RealType>& )
{ // There is no mean:
   return tools::domain_error<RealType>(
      BOOST_CURRENT_FUNCTION,
      "The Cauchy distribution does not have a mean: "
      "the only possible return value is %1%.",
      std::numeric_limits<RealType>::quiet_NaN());
}

This fails at RUN time thus:

Autorun "i:\boost\libs\math\test\math_test\debug\test_cauchy_mean.exe"
Running 1 test case...
unknown location(0): fatal error in "test_main_caller( argc, argv )": std::domain_error: Error in function double __cdecl
boost::math::mean<double>(const class boost::math::cauchy_distribution<double> &): The Cauchy distribution does not have a mean: the
only possible return value is 1.#QNAN.

which is pretty clear, but only at runtime.

If we want it to fail at compile time, IMO it MUST be an option becaue there are occasions when it is vital to be able treat all
distributions alike. A typical case is our C# .NET (cue hisses and boos) 'applet' to calculate distribution parameters - for any
distribution chosen from a dropdown. (This useful program will be posted somewhere soon).

So to provide an option, a macro seems sensible, perhaps BOOST_MATH_COMPILE_FAIL_IF_UNDEFINED?

BOOST_STATIC_ASSERT(false); has been one way of providing a compile time trap and fails with a rather long message, although
clicking on the 1st line goes straight to the source code above, so it soon becomes pretty clear.

"I:\Boost-sandbox\math_toolkit\boost/math/distributions/cauchy.hpp(218) : error C2027: use of undefined type
'boost::STATIC_ASSERTION_FAILURE<x>'
        with
        [
            x=false
        ]
        .\test_cauchy_mean.cpp(700) : see reference to function template instantiation 'RealType boost::math::mean<double>(const
boost::math::cauchy_distribution<RealType> &)' being compiled
        with
        [
            RealType=double
        ]
"

For MSVC at least one can use the #error pragma thus

#ifdef BOOST_MATH_COMPILE_FAIL_IF_UNDEFINED
# ifdef BOOST_MSVC
# error Mean of the Cauchy distribution is undefined!
# else
      BOOST_STATIC_ASSERT(false);
# endif
#endif

and this produces the rather more explicit message:

I:\Boost-sandbox\math_toolkit\boost/math/distributions/cauchy.hpp(216) : fatal error C1189: #error : Mean of the Cauchy
distribution is undefined!

So some questions:

1 Does this macro mechanism seem OK? Is the macro name OK?

2 And should the portable BOOST_STATIC_ASSERT be used?

3 Should the pragma #error be used?

4 What compilers do/don't support #error pragma?

5 Should be default behaviour (macro NOT defined) remain a runtime failure as at present?

Paul

---
Paul A Bristow
Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB
+44 1539561830 & SMS, Mobile +44 7714 330204 & SMS
pbristow_at_[hidden]
 
---
Paul A Bristow
Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB
+44 1539561830 & SMS, Mobile +44 7714 330204 & SMS
pbristow_at_[hidden]
 

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