currency_rounding_problem

currency_rounding_problem - this example Using 10.5 percent...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * From The Register * http://www.regdeveloper.co.uk/2006/08/12/floating_point_approximation / * * Example of a currency calculation with rounding to the nearest cent in * which inexact representation leads to a problem. The Register's example * was $59.00 + 12.5% of $59.00 which has the exact result $66.375 (which * should be then rounded up to $66.38), however inexact representation * leads to a result like 66.374999999, which is rounded down instead. * However I found that my compiler/OS did not have a problem with
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: this * example. Using 10.5 percent did show the problem. */ #include <stdio.h> int main() { float x; /* Currency calculation */ x=59.00+(10.5/100.)*59.00; printf("59.00 + 10.5 percent of 59.00 = %.10f\n", x); printf("The exact answer should be = 65.195\n"); printf("\n"); printf("Consider the effect of rounding to the nearest cent.\n"); /* I get the result 65.1949996948 which would be rounded to $65.19; * however the (exact) answer is 65.195 which would be rounded to * $65.20 - a rip-off of one cent! */ return 0; }...
View Full Document

This note was uploaded on 09/29/2009 for the course COSC 1002 taught by Professor Wheatland during the Three '09 term at University of Sydney.

Ask a homework question - tutors are online