Skip to the content of the web site.

Project I.3: Interval arithemtic

We will now want to perform arithmetic operations on intervals:

  • $[a, b] + [c, d] = $[a + c, b + d]$,
  • $[a, b] - [c, d] = $[a - c, b - d]$,
  • $[a, b] * [c, d] = $[\min{ac, ad, bc, bd}, \max{ac, ad, bc, bd}]$,
  • $[a, b] / [c, d] = $[\min{a/c, a/d, b/c, b/d}, \max{a/c, a/d, b/c, b/d}]$ with it being being a domain error if $c \le 0 \le d$,
  • $-[a, b] = [-b, -a]$.

This will require you to implement six member functions using operator overloading:

class Interval {
	public:
		// Other member functions
		Interval operator+( Interval const &operand ) const;
		Interval operator-( Interval const &operand ) const;
		Interval operator*( Interval const &operand ) const;
		Interval operator/( Interval const &operand ) const;
		Interval operator-() const;
		Interval operator+() const; // Returns the interval unchanged
};

Where possible, many of these functions should be written in the form

Interval Interval::operator+( Interval const &operand ) const {
	Interval result{*this};
	result += operand;
	return result;
};