1

I encountered a strange float number problem of python(2.7.3).

In [4]: 17 * 0.1
Out[4]: 1.7000000000000002

but

In [5]: print(17*0.1)
1.7

Two questions:

  1. why 17*0.1 is 1.7000000000000002 ?
  2. why print is fine?
tidy
  • 4,747
  • 9
  • 49
  • 89
  • 1. IEEE-754 floating point rules. 2. IEEE-754 floating point rules. 3. Consider [two's complement](http://en.wikipedia.org/wiki/Two%27s_complement). – Elliott Frisch Dec 24 '13 at 08:11
  • The `print` is *not* fine. It simply displays the numbers with less digits in order to provide a nicer and more intuitive representation. You generally do not care if the number has an error in the order of 10^-16 or so. If you want to control the number of digits to display than use a format string: `print('{.Nf}'.format(17*0.1))`. Change `N` to change the number of digits to display. – Bakuriu Dec 24 '13 at 08:21

1 Answers1

2

There is a good explanation here:

http://docs.python.org/2/tutorial/floatingpoint.html

James King
  • 6,229
  • 3
  • 25
  • 40