// File: proj3tester.C // // Test main for Project 3, Fixed Point Arithmetic // // NOTE TO GRADERS: Because of the use of floating point numbers, // some students may have answers that are approximations. For example, // the answer may be 12.3400, but the student's answer is 12.3399 // This is fine. // // Also, grade each test based on the STUDENT'S input to the test. That // is, if his/her input to the cast test (i.e., float(value)) is supposed to // be 3.5, but his/her input is (incorrectly) 81.34, grade him/her for the // cast test based on the answer to float(81.34). #include #include "Fixed.H" void main() { // Testing constructor cout << "*** Testing constructor ***\n\n"; Fixed a, b(12.3456), c(12.3456, -2), d(12.3456, 0); Fixed e(12.3456, 1), f(12.3456, 7), g(-12.3456, 5); cout << "[Fixed a -> 0.0]" << endl; cout << "a = " << a << endl; cout << "[Fixed b(12.3456) -> 12.34]" << endl; cout << "b = " << b << endl; cout << "[Fixed c(12.3456, -2) -> 12.34]" << endl; cout << "c = " << c << endl; cout << "[Fixed d(12.3456, 0) -> 12.0]" << endl; cout << "d = " << d << endl; cout << "[Fixed e(12.3456, 1)] -> 12.3" << endl; cout << "e = " << e << endl; cout << "[Fixed f(12.3456, 7) -> 12.3456000 or 12.3456]" << endl; cout << "f = " << f << endl; cout << "[Fixed g(-12.3456, 5) -> -12.34560 or -12.3456]" << endl; cout << "g = " << g << endl; #if 1 // Testing constructor overflow cout << "\n*** Testing constructor overflow ***\n\n"; cout << "[Fixed h(12.34, 9) -> 'precision reduced' msg & h = 12.34000000 or 12.34]" << endl; Fixed h(12.34, 9); cout << "h = " << h << endl; #endif #if 1 // Testing setPrecision cout << "\n*** Testing setPrecision ***\n\n"; cout << "[b = 12.34]" << endl; cout << "b = " << b << endl; cout << "[b.setPrecision(5) -> 12.34000 or 12.34]\n"; b.setPrecision(5); cout << "b = " << b << endl; cout << "\n[f = 12.3456000 or 12.3456]" << endl; cout << "f = " << f << endl; cout << "[f.setPrecision(-4) -> error msg & f = 12.3456000 or 12.3456]\n"; f.setPrecision(-4); cout << "f = " << f << endl; cout << "\n[f.setPrecision(1) -> 12.3]\n"; f.setPrecision(1); cout << "f = " << f << endl; #endif #if 1 // Testing overflow in setPrecision cout << "\n*** Testing overflow in setPrecision ***\n\n"; Fixed i(12.3456, 4); cout << "[i = 12.3456]" << endl; cout << "i = " << i << endl; cout << "\n[i.setPrecision(10) -> 'precision reduced' msg & i= 12.34560000 or 12.3456]" << endl; i.setPrecision(10); cout << "i = " << i << endl; #endif #if 1 // Testing arithmetic operations cout << "\n*** Testing arithmetic operations ***\n\n"; Fixed a1(4321.56, 2), a2(65.4321, 4); Fixed a3(3.12, 2), a4(2.0, 0), answer; cout << "[a1 = 4321.56]" << endl; cout << "a1 = " << a1 << endl; cout << "[a2 = 65.4321]" << endl; cout << "a2 = " << a2 << endl; cout << "[a3 = 3.12]" << endl; cout << "a3 = " << a3 << endl; cout << "[a4 = 2.0]" << endl; cout << "a4 = " << a4 << endl; answer = a1 + a2; cout << "\n[a1 + a2 -> 4386.9921]" << endl; cout << "a1 + a2 = " << answer << endl; answer = a1 - a2; cout << "\n[a1 - a2 -> 4256.1279]" << endl; cout << "a1 - a2 = " << answer << endl; answer = a2 - a1; cout << "\n[a2 - a1 -> -4256.1279]" << endl; cout << "a2 - a1 = " << answer << endl; answer = a2 * a3; cout << "\n[a2 * a3 -> 204.1481" << endl; cout << "a2 * a3 = " << answer << endl; answer = a3 / a4; cout << "\n[a3 / a4 -> 1.56]" << endl; cout << "a3 / a4 = " << answer << endl; #endif #if 1 // Testing overflow in addition cout << "\n*** Testing overflow in addition ***\n\n"; Fixed a5(1.4, 9), a6(1.3, 9); cout << "[a5 = 1.400000000 or 1.4]" << endl; cout << "a5 = " << a5 << endl; cout << "[a6 = 1.300000000 or 1.3]" << endl; cout << "a6 = " << a6 << endl; cout << "\n[a5 + a6 -> 'precision reduced' msg & a5 + a6 = 2.70000000 or 2.7]" << endl; answer = a5 + a6; cout << "a5 + a6 = " << answer << endl; #endif #if 1 // Testing assignment overloading cout << "\n*** Testing assignment overloading ***\n\n"; cout << "[b = g -> b = g = -12.34560 or -12.3456]" << endl; b = g; cout << "b = " << b << endl; cout << "g = " << g << endl; cout << "\n[b = g = a -> b = g = a = 0.0]" << endl; b = g = a; cout << "b = " << b << endl; cout << "g = " << g << endl; cout << "a = " << a << endl; cout << "\n[a = 65.43 -> 65.43]" << endl; a = 65.432; cout << "a = " << a << endl; cout << "\n[a = b = 1.2345 -> a = b = 1.23]" << endl; a = b = 1.2345; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "\n[b = 34.05 -> 34.05]" << endl; b = 34.05; cout << "b = " << b << endl; #endif #if 1 // Testing cast operation cout << "*** Testing cast operation ***\n\n"; cout << "[a = 1.23]" << endl; cout << "a = " << a << endl; cout << "\n[float(a) -> 1.23]" << endl; cout << "float(a) = " << float(a) << endl; cout << "[a still = 1.23]" << endl; cout << "a = " << a << endl; cout << "\n[b = 34.05]" << endl; cout << "b = " << b << endl; cout << "\n[float(b) -> 34.05]" << endl; cout << "float(b) = " << float(b) << endl; cout << "[b still = 34.05]" << endl; cout << "b = " << b << endl; #endif #if 1 // Testing trig operations cout << "\n*** Testing trig operations ***\n\n"; cout << "[c = 12.34]" << endl; cout << "c = " << c << endl; cout << "\n[sin(c) -> 0.22 or 0.22 with more decimal places]" << endl; cout << "sin(c) = " << sin(c) << endl; cout << "\n[cos(c) -> 0.97 or 0.97 with more decimal places]" << endl; cout << "cos(c) = " << cos(c) << endl; cout << "\n[atan(c) -> 1.48 or 1.48 with more decimal places]" << endl; cout << "atan(c) = " << atan(c) << endl; #endif #if 1 // Testing pow operation cout << "\n*** Testing pow operation ***\n\n"; cout << "[e = 12.3]" << endl; cout << "e = " << e << endl; cout << "\n[pow(e, 3) -> 1860.8 or 1860.8 with more decimal places]" << endl; cout << "pow(e, 3) = " << pow(e, 3) << endl; #endif cout << "\n*** End of program. ***\n"; }