/****************************************************************** * BadMoney program * This program demonstrates the inaccuracies that can occur from * using a float to represent money. * NO user input of data is required. * @author J. Dalbey 2/7/2000 * Assignment: Examine the code and write down your prediction of the output. * Then compile and execute the program to obtain the actual results. * If your prediction was incorrect, research the cause and write up a * detailed technical explanation for the observed results. ******************************************************************/ #include int main(void) { float principal; int count; double FloatNumber; int IntegerNumber; /* Example 1 */ float originalPrice = 400000.00f; /* to make the price more attractive, we'll reduce it to $399999.95 */ float reducedPrice = originalPrice - 0.05f; float discount = originalPrice - reducedPrice; /* Display discount amount */ printf("Discount is: %f\n", discount); /* Example 2 */ /* The amount you have in the bank: $12,345,678 */ principal = 12345678.0f; /* Display the original amount */ printf( "The original principal is %f.\n", principal); /* Add a dime to the principal a thousand times */ for (count = 1; count < 1000; count = count + 1) { principal = principal + .10f; } /* The result should be 100 dollars larger. */ printf( "The new principal is %f.\n", principal); /* Example 3 */ FloatNumber = 4.35; IntegerNumber = (int) (100 * FloatNumber); printf("Computation result is %d\n", IntegerNumber); return 0; }