Skip to the content of the web site.

Project I.4: Scalar arithmetic

We will now want to perform arithmetic operations on intervals and scalars:

  • $[a, b] + s = $[a + s, b + s]$,
  • $[a, b] - s = $[a - s, b - s]$,
  • $[a, b] * s = $[\min{as, bs}, \max{as, bs}]$,
  • $[a, b] / s = $[\min{a/s, b/s}, \max{a/s, b/s}]$ with it being being a domain error if $s = 0$,
  • $s / [a, b] = $[\min{s/a, s/b}, \max{s/a, s/b}]$ with it being being a domain error if $a \le 0 \le b$.

This will require you to implement four member functions using operator overloading and four friend functions:

class Interval {
	public:
		// Other member functions
		Interval operator+( double s ) const;
		Interval operator-( double s ) const;
		Interval operator*( double s ) const;
		Interval operator/( double s ) const;

		friend Interval operator+( double s, Interval const &operand );
		friend Interval operator-( double s, Interval const &operand );
		friend Interval operator*( double s, Interval const &operand );
		friend Interval operator/( double s, Interval const &operand );
};

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

Interval Interval::operator+( double s ) const {
	Interval result{*this};
	result += s;
	return result;
};