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 trialHarsh Kumar Woike
204 Pointsfloats
floats always behave like decimals as we use for money? how is it false?
1 Answer
Max Hirsh
16,773 PointsThis is false because of how computers work at a very basic level. Everything a computer does is in base 2 (a number system based on different powers of two, represented as a series of 1's and 0's), but the numbers we use in everyday life are base-10. Because of this, many computer representations of base-10 numbers are only approximations.
For example, 0.100 represented in base 2 is a weird infinite decimal, "0.0001100110011...(base-2)". Here's a link to the number in wolfram alpha: http://www.wolframalpha.com/input/?i=0.100+in+base+2
Because an infinitely long base-2 decimal number is required to represent a simple base-10 decimal number and computers have limited/finite space to store variables, the digital representation of 0.100 (base-10) is only approximate. Because of this, if you run this test:
0.100 * 10 == 1, the answer will be false because the floating point number 0.100 is only approximately 0.1, but not exactly. Sorry for the long-winded explanation but hope this information helps.