3

I'm creating a dictionary with this simple code:

pixel_histogram = {}
min_value = -0.2
max_value = 0.2
interval_size = (math.fabs(min_value) + math.fabs(max_value))/bins

for i in range(bins):
    key = min_value+(i*interval_size)
    print key
    pixel_histogram[key] = 0
    print pixel_histogram

But I'm a little surprised 'cause I got these values with my prints:

#Printing keys
-0.2
-0.16
-0.12
-0.08
-0.04
0.0
0.04
0.08
0.12
0.16

#Printing the dictionary
{0.0: 0, 
-0.08000000000000002: 0, 
0.15999999999999998: 0, 
-0.16: 0, 
0.12: 0, 
-0.12000000000000001: 0, 
0.08000000000000002: 0, 
-0.04000000000000001: 0, 
-0.2: 0, 
0.03999999999999998: 0}

I didn't figure out why the values are different and how could I solve this. Any help would be appreciated. Thanks.

Marcin
  • 48,559
  • 18
  • 128
  • 201
pceccon
  • 9,379
  • 26
  • 82
  • 158
  • 6
    [What every computer scientist should know about floating-point arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Ashwini Chaudhary Sep 23 '13 at 19:57
  • 1
    If you need to use exact floating-point numbers, try using the Decimal class: http://docs.python.org/2/library/decimal.html. – Waleed Khan Sep 23 '13 at 19:58
  • 3
    While that explains the output of the second case, it doesn't explain why the first output differs from the second. –  Sep 23 '13 at 19:59
  • 2
    Related: http://stackoverflow.com/questions/13345334/strange-behaviour-with-floats-and-string-conversion – Ashwini Chaudhary Sep 23 '13 at 20:00
  • Sorry for the noob question. Didn't know about the difference between `str` and `repr`(didn't even know this). Trying to learn more about Python. Thank you guys. – pceccon Sep 23 '13 at 20:15

2 Answers2

8

Python's print statement uses str() on the item being printed. For floating-point values, str() will print up to certain number of decimal values.

When printing a dictionary, the print statement is calling str() on the dictionary object. The dictionary, in its __str__() method definition, uses repr() on the keys. The repr() function for floating-point values prints to more decimal places than the str() function does.

The reason dictionaries use repr() and not str() for keys is that you almost definitely want to see print {'1': 1} print differently than print {1: 1}.

Alok--
  • 724
  • 3
  • 10
  • Thank you @Alok--. This is just what I would like to know. Sorry for the noob question, I didn't know this about the `print` method in Python. – pceccon Sep 23 '13 at 20:13
  • 1
    @pceccon No problem. As an aside, it's not really a good idea to store floating-point values as keys in dictionaries, since that implies that you want to be able to do exact comparison on the keys. (If you know that the values you're going to use as keys in that dictionary can be represented exactly with floating-point values, then it should be OK.) – Alok-- Sep 23 '13 at 20:16
-3

The values are the same. In the first case, they have been truncated or rounded to decimal places. Surely you are familiar with the rounding of numbers?

Marcin
  • 48,559
  • 18
  • 128
  • 201