On Tue, Apr 16, 2013 at 1:48 PM, Jim Mooney <cybervigila...@gmail.com>wrote:
> I accidentally sent as HTML so this is a resend in case that choked > the mailing prog ;') > > I was doing a simple training prog to figure monetary change, and > wanted to avoid computer inaccuracy by using only two-decimal input > and not using division or mod where it would cause error. Yet, on a > simple subtraction I got a decimal error instead of a two decimal > result, as per below. What gives? > > cost = float(input('How much did the item cost?: ')) > paid = float(input('How much did the customer give you?: ')) > change = paid - cost > > #using 22.89 as cost and 248.76 as paid > > twenties = int(change / 20) > if twenties != 0: > twentiesAmount = 20 * twenties > change = change - twentiesAmount > #change is 5.8700000000000045, not 5.87 - how did I get this decimal > error when simply subtracting an integer from what should be a > #two-decimal amount? > because binary computers convert decimal numbers to binary numbers. There is often a rounding error. > print(twenties, ' twenties') > print(change) > > #and so forth for the rest of the prog > > Jim Mooney > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor