/* Filename: rounding.cpp Author: Br. David Carlson Br. Isidore Minerd Date: January 11, 2005 Modified: July 16, 2009 This program tests various ways to implement a rounding function. The program simply outputs a list of numbers along with their rounded values as computed by each rounding function. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET Microsoft Visual Studio 2008 g++ under Linux */ #include #include // Needed for using the pow function. #include // Needed for using setw. using namespace std; // Function prototypes: float Round1(float Value); float Round2(float Value); float Round3(float Value, int NumPlaces); float Round4(float Value, int NumPlaces); int main(void) { float Value; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(5); cout << setw(10) << "Value" << setw(10) << "Round1" << setw(10) << "Round2" << setw(10) << "Round3" << setw(10) << "Round4" << endl; Value = 7.86; cout << setw(10) << Value << setw(10) << Round1(Value) << setw(10) << Round2(Value) << setw(10) << Round3(Value, 2) << setw(10) << Round4(Value, 2) << endl; Value = 2.936; cout << setw(10) << Value << setw(10) << Round1(Value) << setw(10) << Round2(Value) << setw(10) << Round3(Value, 2) << setw(10) << Round4(Value, 2) << endl; Value = 6.164; cout << setw(10) << Value << setw(10) << Round1(Value) << setw(10) << Round2(Value) << setw(10) << Round3(Value, 2) << setw(10) << Round4(Value, 2) << endl; Value = 3.875; cout << setw(10) << Value << setw(10) << Round1(Value) << setw(10) << Round2(Value) << setw(10) << Round3(Value, 2) << setw(10) << Round4(Value, 2) << endl; return 0; } /* Given: Value a floating point number Task: To round Value to 2 decimal places. Return: The rounded number in the function name. */ float Round1(float Value) { float Product, Answer, Fraction; int Whole; Product = 100 * Value; Whole = static_cast (Product); Fraction = Product - Whole; if (2 * Fraction >= 1) return (Whole + 1.0) / 100.0; else return Whole / 100.0; } /* Given: Value a floating point number Task: To round Value to 2 decimal places. Return: The rounded number in the function name. */ float Round2(float Value) { int Temp; Temp = Value * 100.0 + 0.5; return Temp / 100.0; } /* Given: Value a floating point number NumPlaces a positive integer giving the number of decimal places to which to round the answer. Assumes: That NumPlaces is positive. No check is made to see that it really is. Task: To round Value to NumPlaces decimal places. Return: The rounded number in the function name. */ float Round3(float Value, int NumPlaces) { float Factor; int Temp; Factor = pow(10.0, NumPlaces); Temp = Value * Factor + 0.5; return Temp / Factor; } /* Given: Value a floating point number NumPlaces a positive integer giving the number of decimal places to which to round the answer. Task: To round Value to NumPlaces decimal places. If NumPlaces is not postive, Value is returned unchanged as the answer. Return: The rounded number in the function name. */ float Round4(float Value, int NumPlaces) { int k, Temp; float Factor; Factor = 1; for (k = 0; k < NumPlaces; k++) Factor = Factor * 10; Temp = Value * Factor + 0.5; return Temp / Factor; }