Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (Retired) Things That Count Basic Numbers

Herb Bresnan
Herb Bresnan
10,658 Points

Why do floats behave the way they do?

I know this is off topic, and beyond the scope of this course, but too curious to let it go. Why does 0.1 + 0.1 + 0.1 - 0.3 = 5.5511......?

2 Answers

Seth Kroger
Seth Kroger
56,414 Points

Floats are stored in binary (or Base-2) just like other numbers on computers. Instead of fractional digits being based on 1/10th's, 1/100th's, 1/1,000th's, etc. they are based on 1/2's, 1/4's, 1/8's, etc. This means that can only provide exact fractions for factors of 2 but not 10 (which factors to 2 and 5).

It's the same reason 1/3 in decimal is 0.33333333333333... Because 3 isn't a factor of 10 you can only have an approximate value in decimal, not an exact value, with the accuracy depending on how may digits you write the number out to.

Herb Bresnan
Herb Bresnan
10,658 Points

Thank you Seth. I need to remind myself to try to think like a programmer and this is not basic math.