asf107
asf107

Reputation: 1146

python float formatting weirdness?

I'm trying to debug two different python scripts that execute very similar code. These scripts set a, which is a simple float.

For script 1:

ipdb> print sys.version
2.7 (r27:82500, Jul 15 2010, 13:04:38)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)]
ipdb> type(a)
<type 'float'>
ipdb> print a
17.23105
ipdb> print '{0:.4f}'.format(a)
17.2311

For script 2:

ipdb> print sys.version
2.7 (r27:82500, Jul 15 2010, 13:04:38)
ipdb> print type(a)
<type 'float'>
ipdb> print a
17.23105
ipdb> print '{0:.4f}'.format(a)
17.2310

How is it possible that a formats differently in the two cases? In what ways can a possibly differ between the two scripts, if I've already checked that their value is the same at 17.23105 ? (unfortunately the python code that originally sets a is very, very long... I wouldn't want to bore everyone with 1000+ lines)

Upvotes: 3

Views: 261

Answers (1)

user2357112
user2357112

Reputation: 280837

The str representation of a float truncates to 12 digits on Python 2. This is unfortunate. You have to print repr(a) to make sure you see enough precision to uniquely identify the float. There's probably a tiny difference that only shows up after 12 digits.

On Python 3, str and repr produce the same output for floats, so you wouldn't have this problem.

Upvotes: 6

Related Questions