0

I wrote those simple lines and run them on python 2/3.

i=1.194857193845710948754654
print(len(str(i)))

I got 2 different outputs. What is the reason for that? and how could I achieve the same output in python2 ?

Output python2:

>>> 13

Output python3:

>>> 18
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
fu2y
  • 1
  • 1
  • 1

1 Answers1

3

It's because of different implementation of str() for float, as can easily be verified.

The actual float precision is the same.

$ python2
Python 2.7.9
>>> i=1.194857193845710948754654
>>> i
1.1948571938457109
>>> str(i)
'1.19485719385'
>>> 
$ python3
Python 3.4.3
>>> i=1.194857193845710948754654
>>> i
1.1948571938457109
>>> str(i)
'1.1948571938457109'
>>> 
MightyPork
  • 18,270
  • 10
  • 79
  • 133
  • thanks a lot, this solved my problem. I didnt thought the string conversion could be the limiting factor :) – fu2y Mar 21 '15 at 13:53