Ask your own question, for FREE!
MIT 6.01SC Introduction to Electrical Engineering and Computer Science I 25 Online
OpenStudy (anonymous):

Please look at the following code from python shell: >>> a = float(2.8) >>> print a 2.8 >>> a 2.7999999999999998 Can anyone explain me the reason for the difference?

OpenStudy (anonymous):

In computing, floating point describes a method of representing real numbers in a way that can support a wide range of values. Numbers are, in general, represented approximately to a fixed number of significant digits and scaled using an exponent. The base for the scaling is normally 2, 10 or 16. The typical number that can be represented exactly is of the form: \[Significant digits × base ^{exponent}\] Because \[2.8 = 1*2^{1}+0*2^{0}+\]\[+1*2^{-1}+1*2^{-2}+0*2^{-3}+0*2^{-4}+1*2^{-5}+1*2^{-6}+0*2^{-7}+0*2^{-8}+\]\[+1*2^{-9}+1*2^{-10}+0*2^{11}+0*2^{-12}+1*2^{-13}+1*2^{-14}+0*2^{-15}+0*2^{-16}+\]\[+1*2^{-17}+1*2^{-18}+0*2^{-19}+0*2^{-20}+1*2^{-21}+1*2^{-22}+0*2^{-23}+0*2^{-24}+ ...\] The first 24 terms (24 is the length of the mantisse) add, more or less, 2,79999995231 . See conversions in http://www.h-schmidt.net/FloatConverter/ Always we need more than 24 digits to convert a decimal to base 2, we'll have an error because the mantissa length is limited (24 bits for float and 52 bits for double precission).

OpenStudy (anonymous):

Explains a lot, thanks.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!