[Tutor] quick question

Brett Ritter swiftone at swiftone.org
Fri Sep 28 07:26:13 CEST 2012


On Thu, Sep 27, 2012 at 10:18 PM, jh <xperimental22 at gmail.com> wrote:
> The subtotal of your items is: 26010.850000000002
>
> My question here is, why does my subtotal have so many decimals when I never
> went above 2 in my input?

This is not actually a Python thing, it's a computer thing.  Computers
represent numbers (everything) in binary, as you doubtless have heard.
 The issue is that while 1 or 12 or 4562 are easily represented in
binary, a number like "0.1" is less obvious.  Floating point numbers
are stored as binary approximations that dont' work out to exactly the
same thing.  (For _most_ purposes they are close enough, but if you
are every dealing with highly precise math, there are libraries to
help be more accurate)

This is similar to how 1/3 can't be represented easily in decimal
form.  (3 is hard number to use as a divisor in base 10.  It turns out
that most digits are painful to use as divisors in base 2 (binary)
except for 1,2,4, 8, or other powers of 2.)

-- 
Brett Ritter / SwiftOne
swiftone at swiftone.org


More information about the Tutor mailing list