![]() | CoSc2030: Computer Science IIFall Semester 2007Instructor: Dr. Thomas Bailey |
We usually say that computers are better than people at "number crunching". But as this lab demonstrates, there are several issues that programmers need to take into account when performing computer arithmetic. During the lab lecture we talked about the problem of numeric overflow and precision limitations of floating point number representation. This lab gives you a chance to investigate the problem in context of several calculations.
. Experiment with several values for the upper limit (n in the formula). Detect the overflow by outputing the results to the console and report the value of n that produces the overflow. (Hint: try values around 250).
Use float to store the result of the computation, and experiment with several values for n. How can you detect an overflow in this case? You might find it helpful to consult the Visual Studio online help. (Hint: unlike integer types, floating point numbers have a special bit pattern reserved for infinity, so a float variable overflows when it stops being finite.)
. Try it out with different values of n and experiment with float and double as the data types to store the result. (Try computing both sum = 1 and sum = -1) Can you explain the error?for (float i = 3.4; i < 4.4; i += 0.2) { cout << "i = " << i << endl;}Upload your source code in a file Lab2NumericErrors.cpp with the implementation of the requested functions. Include your answers to the above questions as comments in the code along with your name.
<< CoSc2030 Home