class Interval {
private:
double low; // lower bound of interval
double upp; // upper bound of interval
public:
Interval() { low = upp = 0.0; } // class constructors
friend Interval add(Interval, Interval, Interval &);
// utility function
};
Interval operator + (Interval a, Interval b)
// overloaded addition operator
{
Interval c;
add(a, b, c); // call appropriate utility function
return c; // return sum of a and b
}
Software rounding using the
is
implemented by overloading the arithmetic operators as shown in the
following example for addition:
Interval add(Interval a, Interval b, Interval &c)
{
double low = a.low + b.low; // calculate the lower bound
double upp = a.upp + b.upp; // calculate the upper bound
c.low = low - ulp(low); // extend the lower bound by ulp
c.upp = upp + ulp(upp); // extend the upper bound by ulp
}
where
is the function described previously for
calculating the
.
Hardware rounding is implemented by overloading the arithmetic operators as follows:
Interval add(Interval a, Interval b, Interval &c)
{
swapRM(ROUND_TO_MINUS_INFINITY); // set round to -infinity mode
c.low = a.low + b.low; // calculate the lower bound
swapRM(ROUND_TO_PLUS_INFINITY); // set round to +infinity mode
c.upp = a.upp + b.upp; // calculate the upper bound
}
where
is the SGI-specific function for setting
the IEEE-754 rounding mode. (Although requiring the implementation of
the four rounding modes, the standard does not specify the mechanism
by which the modes are set.)
The software rounding method is computationally more expensive than hardware rounding, requiring an extra addition and subtraction and the computation of the of two values. Note that the software rounding method extends the upper and lower bounds of the interval during every arithmetic operation; the hardware rounding method only extends the bounds when the result of the operation cannot be exactly represented, producing tighter interval bounds. Thus, the relationship between an infinite precision value and its interval under rounding is , while for hardware rounding it is .