[Tutor] How did this decimal error pop up?

Jay Lozier jslozier at gmail.com
Wed Apr 17 03:49:03 CEST 2013


On 04/16/2013 01:43 PM, Jim Mooney wrote:
> I was doing a simple training prog to figure 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?
> *  print(twenties, ' twenties')
>   print(change)
>
> #and so forth for the rest of the change
>
> -- 
> *Jim Mooney
> * 
This is a common problem with decimal numbers that is discussed in 
various numerical methods texts and is independent of the programming 
language. The two numerical texts I have from the 1980's discuss this 
problem using FORTRAN and Pascal ( I am fairly certain they are out of 
print). It caused by binary representation of decimal numbers at times 
will introduce errors in decimal numbers. Many decimal numbers can not 
be exactly represented by a binary number with some binary 
representations being a little higher and some a little lower than the 
actual number. There are recommend algorithms and data encoding 
techniques to use to minimize these problems.

Integers always have an exact binary representation so if it one can 
change the problem to one that uses integers instead of floats then the 
rounding problem disappears. Many financial calculations can be 
converted from dollars to cents to avoid decimals. Unfortunately, many 
science and engineering calculations will need to address the decimal to 
binary to decimal representation errors.

It can be a nasty problem if one does not address it properly an even 
larger error could occur. Often it is suitable error because one does 
not spot that an intermediate calculation is incorrectly implemented.

-- 
Jay Lozier
jslozier at gmail.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130416/568e896d/attachment.html>


More information about the Tutor mailing list