Floating point problem

Chris Angelico rosuav at gmail.com
Sat Apr 18 15:42:36 EDT 2020


On Sun, Apr 19, 2020 at 4:26 AM Grant Edwards <grant.b.edwards at gmail.com> wrote:
>
> On 2020-04-18, Souvik Dutta <souvik.viksou at gmail.com> wrote:
> > I literally tried it!!! And it did not stop because I did not get any 1.0
> > rather I got 0.99999999999 But why does this happen. This is a simple math
> > which according to normal human logic should give perfect numbers which are
> > not endless. Then why does a computer behave so differently?
>
> Because computers _don't_do_math_.  That is a very important thing to
> remember.
>
> Computer do something that _approximates_ math... in some
> situations...  if you know what you're doing.

That's not true in the absolute. Computers DO do math. But like
people, they can't do more than approximate to infinities. A computer
can do true math as long as it involves integers or rationals that are
small enough to fit in its memory - just like humans do.

> In you're case you're doing IEEE floating point operations. Before you
> use floating point, you should read the article by Goldman that has been
> suggested:
>
> https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
> https://dl.acm.org/doi/10.1145/103162.103163
>

IEEE floating-point is indeed a variant form of fundamental
mathematics, though, so in this specific case, you're not wrong. But
in general (and, for instance, when you use Python's int or Fraction
types), computers *can* do math.

ChrisA


More information about the Python-list mailing list