Skip to the content of the web site.

Project I.5: Functions on intervals

The functions that appear in the standard math library could also be written for taking intervals as an argument.

For a monotonic function such as exp(...), the result is easy enough to calculate. For monotonic functions with a restricted domain such as sqrt(...) and log(...), it may be necessary to throw a std::domain_error exception.

The work required is more significant when one considers the functions that are not monotonic. How can you tell whether or not $\sin([a, b])$ should be $[\sin(a), \sin(b)]$, $[\sin(b), \sin(a)]$, $[\sin(a), 1]$, $[\sin(b), 1]$, $[-1, \sin(a)]$, $[-1, \sin(b)]$, or $[-1, 1]$? You could first look at the magnitude $b - a$, and if this greater or equal to $2\pi$, then the result must be $[-1, 1]$. Otherwise, it may be more difficult.

Take some time to think about this, and then come back and read the next hint.

You can determine the result of $\sin([a, b])$ simply by comparing the relative magnitudes of $\sin(a)$, $\cos(a)$, $\sin(b)$ and $\cos(b)$. Recall that the derivative of $\sin$ is $\cos$.

Try to adapt as many of the functions in the standard math library to function with intervals. For some functions, it makes no sense to implement them, so give your reasons why.