< previous page page_558 next page >

Page 558
{
     return Funct(leftEdge + width / 2.0) * width;
}

//******************************************************************

float Funct( /* in */ float x )   // Value to be cubed

// Computes x cubed. You may replace this function with any
// single-variable function

// Precondition:
//     The absolute value of x cubed does not exceed the
//     machine's maximum float value
// Postcondition:
//     Function value == x cubed

{
    return x * x * x;
}
Testing: We should test this program with sets of data that include positive, negative, and zero values. It is especially important to try to input values of 0 and 1 for the number of divisions. The results from the program should be compared against values calculated by hand using the same algorithm and against the true value of the area under the curve of X3, which is given by the formula
0558-01.gif
(This formula comes from the mathematical topic of calculus. What we have been referring to as the area under the curve in the interval a to b is called the integral of the function from a to b.)
Let's consider for a moment the effects of representational error on this program. The user specifies the low and high values of the interval, as well as the number of subdivisions to be used in computing the result. The more subdivisions used, the more accurate the result should be because the rectangles are narrower and thus approximate more closely the shape of the area under the curve. It seems that we can obtain precise results by using a large number of subdivisions. In fact, however, there is a point beyond which an increase in the number of subdivisions decreases the precision of the results. If we specify too many subdivisions, the area of an individual rectangle becomes so small that the computer can no longer represent its value accurately. Adding all those inaccurate values produces a total area that has an even greater error.

 
< previous page page_558 next page >