
Boost : 
From: Guillaume Melquiond (gmelquio_at_[hidden])
Date: 20030411 07:05:38
On Fri, 11 Apr 2003, Paul A. Bristow wrote:
> In sucessfully using intervals with Stats code from Neal Becker, I had some
> minor dificulties:
>
> 1 abs
>
> It took me some fumbling to see that I needed
>
> using namespace boost::numeric; // BEFORE
> #include "stats.hpp"
It shouldn't be needed. 'abs' is defined in the same namespace than
'interval' so the name resolution should find the correct version of
'abs'. Am I missing something here?
> In doing do, I found that
>
> typedef boost::numeric::interval<double> I;
> I x = 2.;
> I x = boost::numeric::abs(x);
>
> works as expected, but I was puzzled that
>
> using boost::numeric::abs;
> T absx = abs(x); // failed (with MSVC 7.0) because interval policies not found.
Is it only a typo in your mail or did you really try to convert the result
of 'abs(x)' to the type 'T'? The function 'abs' computes:
abs(X) == {abs(x)  x in X}
In the next version of the library, there will be a new function called
'norm' that returns:
norm(X) = max {abs(x)  x in X}
Maybe it's this kind of behavior you were expecting?
> I absx = boost::numeric::abs< boost::numeric::interval<double> >(x);
>
> testInterval.cpp(96) : error C2783: 'boost::numeric::interval<T,Policies>
> boost::numeric::abs(const boost::numeric::interval<T,Policies> &)'
> : could not deduce template argument for 'Policies'
In fact, the definition of 'abs' is:
template<class T, class Policies> inline
interval<T, Policies> abs(const interval<T, Policies>& x)
So if you want to fully qualify the expresion, it should be:
I absx = boost::numeric::abs< double, I::traits_type >(x);
But the compiler should be able to do it for you.
> or
>
> I absx = abs(x);
>
> testInterval.cpp(96) : error C2664: 'abs' : cannot convert parameter 1 from 'I'
> to 'int'
> No userdefinedconversion operator available that can perform this
> conversion, or the operator cannot be called
One more time, the compiler doesn't find the correct version of 'abs'.
Since I don't use MSVC 7, I can't really help you. But I think it's a bug
in the compiler.
> 2 I found several places where expressions like T mean/integer count failed
> when T was an interval type. This is because there is no conversion from
> integer, though there is from double etc. writing mean/T(count) works OK.
>
> I half_x = x/2; // fails
> I half_x = x/2.0; // OK
> I half_x = x/ I(2); // OK
>
> This is a very common situation and would be helpful to avoid widespread changes
> to existing code  but perhaps this is an exercise for the student :)
Yes, there are only mixed operations between expressions of type
'interval<T,_>' and 'T'. So it is normal for the first expression to fail.
The correct way to do it is:
I half_x = x/T(2);
We didn't include automatic promotion of 'int' to 'T' or 'interval<T,_>',
because the library don't have any information of the validity of the
conversion between 'int' and 'T'. If you think it is needed, we can add
it.
> 3 I found
>
> using boost::numeric::pow;
> x1 = pow(x, 2); // multiplicative_inverse undeclared in arith2.hpp
>
> required addition of explicit interval_lib::
>
> return interval_lib::multiplicative_inverse(pow(x, pwr));
> // return multiplicative_inverse(pow(x, pwr)); // was
Thank you, I forgot this one. I will change it.
> 4 x1 = exp(x); // fails  exp_up and exp_down not a member ...
>
> I:/boost_1_30_0\boost\numeric\interval\transc.hpp(39) : error C2039: 'exp_up' :
> is not a member of 'boost::numeric::interval_lib::rounded_math<T>'
> with
> [
> T=double
> ]
> I:/boost_1_30_0\boost\numeric\interval\hw_rounding.hpp(50) : see
> declaration of 'boost::numeric::interval_lib::rounded_math<T>'
> with
> [
> T=double
> ]
Yes, it's normal. Because we don't expect the mathematical functions to be
correctly rounded (and so the results would not be correct), the exp_up
and exp_down are not provided by default. In the first version of the
library, there were placeholders for these functions. But during the Boost
submission, the reviewers explained they prefered the compiler to return a
compilation error rather than the functions to return an unusable result
('exp(x)' would have been '<0,+inf>'). So we removed them.
> 5 similarly with log_up and log_down.
Same explanation.
> 6 Using interval pi I was puzzled that:
>
> using boost::numeric::interval_lib::pi< boost::numeric::interval<double> >; //
> triggers compile fail at pi<I>()
> using boost::numeric::interval_lib::pi; // illegal use of expression
> I pi_d = pi<I>();
>
> whereas
>
> using namespace boost::numeric::interval_lib;
> I pi_d = pi<I>();
> and
>
> I pi_d = boost::numeric::interval_lib::pi< boost::numeric::interval<double>
> >();
>
> are fine.
I'm not sure to understand what you mean. Is it supposed to be another
deficiency of the compiler? Or is there a problem with the library?
> But overall I hope this shows a reallife use of interval arithmetic which I
> believe has considerable potential usefulness.
>
> Thanks
>
> Paul
Thank you for using the library
Guillaume
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk