|
Boost : |
From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-05-05 07:29:19
>From: "Guillaume Melquiond" <gmelquio_at_[hidden]>
> On Mon, 5 May 2003, [iso-8859-1] Terje Slettebø wrote:
>
> > The Interval library, which has recently been accepted into Boost, has
been
> > mentioned regarding the discussion of an SI units/quantity library. I've
> > read the docs for this, and it appears that no special support is needed
to
> > use this with a quantity library, provided the library may use any type
as
> > the underlying type. In that case, you can just use interval<T> rather
than
> > T, and you can use it with interval arithmetic.
> >
> > Interval arithmetic is useful to specify precision, and other cases
where
> > you're dealing with an interval.
> >
> > When it comes to the output operator, it displays an interval in the
form
> > "[<lower>, <upper>]". This is fine as a general output.
>
> As I explained in a previous mail, interval/io.hpp is only here as a
> debugging tool, it is not included by interval.hpp, and each user should
> define its own versions of >> and <<. As I suggested before, I am even
> ready to suppress this file if it could clear any misunderstanding on its
> purpose.
Ah, sorry. As I hadn't followed the Interval library formal review, I
started reading the postings in that thread, but as there were a lot of
postings, I didn't read that much, there, and instead read the docs.
The proposed output operator may be more general-approach, though, for the
cases where intervals are used to specify precision, so it might be useful
to provide it in some way, rather than each user having to make it.
> > interval is used to determine precision, it may be more useful to print
the
> > value, with the number of significant digits, instead. For example:
> >
> > std::cout << interval<double>(1.231, 1.233); // Prints "1.23"
> >
> > std::cout << interval<double>(1.2345678911, 1.2345678913); // Prints
> > "1.234567891"
>
> It is a nice idea and could probably be useful in some particular
> situations.
Yes, when using intervals to specify precision, which according to the docs
is one of the motivations for the Interval library. Especially in scientific
calculations, to ensure that the result isn't given with more significant
digits than warranted by the values used.
Paul A. Bristow asked in the quantity-thread if the quantity libraries could
be used with the Interval library, which is why I looked into it.
For example, using it, you may do like this:
meter<interval<double> > length(10.00, 10.01);
second<interval<double> time(3.000, 3.001);
std::cout << length/time; // Prints 3.33 m/s
Had the Interval library not been used, it would have printed "3.333333
m/s", a precision not warranted by the values used.
One of way you may fail a science test at school is to use more digits in
the answer than what is warranted by the values used. As you know, it's an
important concept in science, similar to using correct units in
calculations, which would be taken care of by a quantity library. Thus, they
work well together.
The way to specify number of significant digits is a little obscure, though.
Being able to do "interval<double> value(<value>, <number of significant
digits>)" could have been easier to use, but a two-argument constructor
already exist for specifying the lower and upper values, and using intervals
for precision is only one application of it, so it may be better to have
something like this outside the library. For example something like:
std::cout << make_interval<double>("10.0")/make_interval("3.00"); // Prints
3.33
This would also make it possible to define an input operator, using the same
routine.
> I have some comments to do about this code. First, the interval library
> don't lie anymore in the boost directory, it is in boost/numeric. And
> similarly, the class is in the namespace boost::numeric.
>
> Next, the signature of the operator should be changed to deal with
> interval policies (only the definition need to be changed, the code can
> stay as it is):
>
> template<class T, class Policies, class CharType, class CharTraits>
> std::basic_ostream<CharType, CharTraits> &operator<<
> (std::basic_ostream<CharType, CharTraits> &stream,
> const interval<T, Policies> &value)
This is fixed, now.
> Another point: your operator expects the two bounds of the interval to be
> of the same sign. If the interval contains 0, the answer won't have a lot
> of meaning. Moreover, if one of the bounds is 0, the behavior probably
> is undefined.
Ok. Is there any way to fix this? I've attached the new version of the
operator, and a test file.
> Finally, there might be a lot of problems with respect to rounding policy.
> But I don't really want to speak about them: it is kind of an internal
> problem to the library and it would be important only if the users expect
> the operators >> and << to behave like the arithmetic operators.
In what way do you mean arithmetic operators? Do you mean bit-shift? It
should be clear from the context that it's a stream operator.
> > Could this be a useful addition to the Interval library?
>
> Yes, it could be. But I am not sure if it is an useful addition to the
> headers part, or if it should rather go to the examples part.
That would be fine.
> > Since there is an output operator in boost/interval/io.hpp, this might
be
> > called boost/interval/io_precision.hpp.
>
> Supposing it goes to the headers part, I would better create an
> interval/io directory and put all the io schemes inside. So the file would
> become interval/io/precision.hpp. However, the name "precision" does not
> ring any bell to me; am I the only one or should a more explicit name be
> found?
I'd be open for any suggestions, as well. The name chosen was just off the
top of my head.
Regards,
Terje
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk