Question: Consider the example code fp precision.cpp. For this problem, you need to determine the minimum loop bound for which the program will go into an
Consider the example code fp precision.cpp. For this problem, you need to determine the minimum loop bound for which the program will go into an infinite loop.
You can derive the loop bound by analyzing the floating-point bit representation and reasoning about the trade-offs in range and precision. You want to find the smallest floating-point value for which an increment of one is lost because there is not enough bits in the fraction part. Show all steps in your derivation.
Once you have derived the loop bound analytically, you can validate your results using with the example code.
You may find the C++ bitset class helpful. The class provides a convenient mechanism for printing out the bit representation of a floating-point value. It may also help to breakdown the loop bound into a power-of-two value.
EXAMPLE CODE:
#includeusing namespace std; const float LOOP_BOUND = 1e+08; // hundred million const unsigned long PRINT_INTERVAL = 1e+06; // one million int main(int argc, char *argv[]) { float i = 0; int j = 0; while (i <= LOOP_BOUND) { // print counter value every millionth iteration if (j % PRINT_INTERVAL == 0) cout << i << endl; i++; j++; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
