HELP: restore my faith in Python

Alex alex at somewhere.round.here
Fri Mar 3 14:43:02 EST 2000


I think it's a matter of small numerical errors:

>>> target = 6
>>> n=((target/(limit+0.0))-(target/limit))*limit
>>> n
1.0
>>> '%.50f' % n
'0.99999999999999977795539507496869191527366638183594'

I think that by default, python rounds off the output of float to 10
decimal places or something.

I guess the error is in the division:

>>> '%.50f' % (6/5.0)
'1.19999999999999995559107901499373838305473327636719'

If this erodes your faith in python, you should also cast a dubious eye
towards C, which this output is based on.  E.g., this program:

#include <stdio.h>

int main(){
  printf("%.50f\n", 6/5.0);
}

gives the output 1.19999999999999995559107901499373838305473327636719,
as well.

Alex.



More information about the Python-list mailing list