[Tutor] Floating Point Craziness

Steven D'Aprano steve at pearwood.info
Tue Jun 14 01:16:00 CEST 2011


Kĩnũthia Mũchane wrote:
> On 06/12/2011 08:13 PM, Steven D'Aprano wrote:

>> Unfortunately, many common fractions cannot be written exactly in 
>> binary. You're probably familiar with the fact that fractions like 1/3 
>> cannot be written exactly in decimal:
>>
>> 1/3 = 0.33333333... goes on forever
> Does it? :-)

Yes.

0.3 is not 1/3, it is 3/10, not 3/9.
0.33 is not 1/3, it is 33/100, not 33/99.
0.333 is not 1/3, it is 333/1000, not 333/999.
...
0.3333333333 is not 1/3, it is 3333333333/10000000000 instead of 
3333333333/9999999999.


And so forth. No matter how many 3s you have, it will always be just 
short of 1/3. Anything less than an infinite number of decimal places is 
not 1/3 exactly, but some approximation less than 1/3.



-- 
Steven


More information about the Tutor mailing list