Boost logo

Boost :

Subject: Re: [boost] boost interval arithmetic
From: Павел Кудан (coodan_at_[hidden])
Date: 2015-01-14 16:44:33


>
>I tried the following program:
>
>#include <boost/numeric/interval.hpp>
>#include <iostream>
>
>typedef boost::numeric::interval<double> I;
>int main(){
>   I a(1,2);
>   I b(-1,1);
>   std::cout << lower(a) << ',' << upper(a) << std::endl;
>   std::cout << lower(b) << ',' << upper(b) << std::endl;
>   a/=b;
>   std::cout << lower(a) << ',' << upper(a) << std::endl;
>}
>
>and it prints:
>
>1,2
>-1,1
>-inf,inf
>
>and not -inf,-1 as you claim. If you have code showing a different result,
>please post it. I can't see anything in the documentation that supports
>your claim.
>
>[-inf,inf] is the smallest interval containing all possible values for the
>quotient, so it makes a lot of sense to return that. Now, if I understand
>correctly, you would like to add a policy so that anytime an operation has
>a disconnected image, instead of bridging the gap, boost would throw. That
>would be useless in my applications (well, maybe a shortcut since once I
>get [-inf,inf] somewhere it tends to propagate, eventually to a comparison
>that will throw), but maybe it makes sense for yours... In any case, it
>doesn't look like it would hurt boost to add such an optional policy. So
>if you want to prepare a patch, someone might be willing to review and
>pull it.
>
>-
In that case [-inf, inf] should be a result of any operation in the world :) As it is universal :)))

2 + 2 == [-inf, inf], is it true? :)

Of course - as it is somewhere between :)

Not true? Of course, not.

2 + 2 !=  [-inf, inf]
and [-inf, inf] != [-inf, 1] U [1, inf]

The behaviour, that you just found and proved by test example is bug of misconception implementation. As in that case boost interval class conflict with own documentated conception (which is partly wrong).

It looks like things with boost interval class are in fact even worse that it seems to be...

Sorry for creating parrallel subthreads, so I repeat here some proofs from boost interval documentation:

Let's see presentation about boost interval and interval arithmetic.

https://www.lri.fr/~melquion/doc/03-rnc5-expose.pdf

On page 11:
Functions can also be used to compute a pair of
intervals: [1, 2] ÷ [−1, 1] = [−∞, −1] ∪ [1, +∞]. (that is not boost interval operator / yet, it is basic arithmetic operation for interval. That is what should be a result)

also, on page 5:
[a, b] × [c, d] = [min(ac, bc, ad, bd), max(ac, bc, ad, bd)],
[a, b] ÷ [c, d] = [a, b] × [1 ÷ d, 1 ÷ c] if 0 NOT ∈ [c, d]

(that is not boost interval operator / yet, but it is the way its result is calculated when zero is not inside of [c, d] interval (by the way, typo here - must be 'if 0 NOT ∈ (c, d)).
And here official boost interval documentation

http://www.boost.org/doc/libs/1_37_0/libs/numeric/interval/doc/interval.htm

The operators / and /=  will try to produce an empty interval if the denominator is exactly zero. If the denominator contains zero (but not only zero), the result will be the smallest interval containing the set of division results; so one of its bound will be infinite, but it may not be the whole interval.

So, official documented result of / operator for that case is [-inf, 1] as it is smallest interval (the result of division_part1() function of boost interval class). And this result is not correct as it is not full.


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