[Tutor] while loop ends prematurly

Steven D'Aprano steve at pearwood.info
Mon Jan 2 07:28:39 CET 2012

Dave Angel wrote:

> Easiest answer is to use integers.  Scale everything up by a factor of 
> 100, and you won't need floats at all.  Just convert when printing (and 
> even then you may get into trouble).
> Another answer is to use Decimal class, which CAN represent decimal 
> values exactly.

That only applies to decimal values which can be represented using a fixed 
number of decimal places. So 1/5 is fine, and is 0.2 exactly, but 1/3 is not, 
since it would require an infinite number of decimal places.

> BTW, if this is supposed to represent US legal tender, you left out the 
> fifty-cent piece as well as the two dollar bill.



More information about the Tutor mailing list