#P1812. Interval Arithmetic Operations
Interval Arithmetic Operations
Interval Arithmetic Operations
In interval arithmetic, rather than representing a number as a single precise value, a quantity is expressed as an interval with a lower and an upper bound. For instance, an interval \([3,5]\) represents all the points on the number line between 3 and 5. A precise number 5 can be represented as the degenerate interval \([5,5]\).
The operations between two intervals are defined as the set of results obtained by applying the operation on every pair of elements from the two intervals. For example:
- Negation: \(-[-3,5] = [-5,3]\)
- Addition: \([3,5]+[-10,1] = [-7,6]\)
- Subtraction: \([3,5]-[-10,1] = [2,15]\)
- Multiplication: \([3,5]\times[-10,1] = [-50,5]\)
- Division: \([3,5]\div[-10,-0.1] = [-50,-0.3]\)
Your task is to implement a program that takes a single-line expression representing one of the above interval operations and computes the resulting interval.
The expression can either be a unary negation, e.g. -[-3,5]
, or a binary operation between two intervals in the format [a,b] op [c,d]
, where op
can be +
, -
, ×
(multiplication), or ÷
(division). In the case of binary operations:
- Addition: \([a,b]+[c,d] = [a+c, b+d]\)
- Subtraction: \([a,b]-[c,d] = [a-d, b-c]\)
- Multiplication: \([a,b]\times[c,d] = [\min(ac, ad, bc, bd), \max(ac, ad, bc, bd)]\)
- Division: \([a,b]\div[c,d] = [\min(a/c, a/d, b/c, b/d), \max(a/c, a/d, b/c, b/d)]\), provided that 0 is not contained in the second interval.
inputFormat
The input consists of a single line containing an expression. The expression is either a unary operation in the form -[a,b]
or a binary operation in the form [a,b] op [c,d]
where op
is one of +
, -
, ×
, or ÷
. The numbers a, b, c, and d can be integers or decimals. It is guaranteed that in case of division, 0 will not be within the interval [c,d].
outputFormat
Output the resulting interval in the format [x,y]
without any extra spaces, where x is the lower bound and y is the upper bound of the result.
sample
-[-3,5]
[-5,3]