[Tutor] strange output

Benoit Dupire bdupire@seatech.fau.edu
Thu, 26 Apr 2001 12:22:48 -0400


Computer store numbers using a binary representation.
For example,  a= 1101 0101. So if you decide to have 4 digits for the fractional
part, this number can be converted to decimal in this
way
trunc(a) = 1101  ---> trunc (a) = 1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 13
fract(a) = 0101  --> fract (a) = 0 * 2^(-1) + 1 * 2^(-2) + 0 * 2^(-3) + 1*
2^(-4)
                                           = 0 * 1/2 + 1 * 1/4 + 0 * 1/8 + 1 *
1/16
As you can see, the precision obtained is 1/16.
a = 13.3125

Tne number just prior to 'a' is 1101 0100, which is 13.25 in decimal.
You can see that 13.3 has no correct representation, but it's not  related to
Python, just to the way numbers are represented by the
computer.


Benoit

Remco Gerlich wrote:

> On  0, Pijus Virketis <virketis@fas.harvard.edu> wrote:
> > Hi,
> >
> > not that this is very significant, but perhaps someone can explain this
> > curious bit of output?
> >
> > Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
> > Type "copyright", "credits" or "license" for more information.
> > IDLE 0.8 -- press F1 for help
> > >>> 4.2-2.5
> > 1.7000000000000002 <-- ?!? Where do we get this miraculous digit?
> >
> > >>># just for comparison ...
> > >>> 7.0-4.0
> > 3.0 <-- All's well ...
> > >>>
>
> This has to do with the inherent inaccuracy of floating points that are
> represented by a fixed number of binary digits. '4.2' has no precise
> representation, neither has '1.7'.
>
> I finally got tired of answering this question so I added an explanation to
> the FAQ, some other people should review it too since it's early morning and
> I'm sure it could be worded better, but it's really time an explanation
> for this is in there:
> http://www.python.org/cgi-bin/faqw.py?req=show&file=faq04.098.htp
>
> --
> Remco Gerlich
>