0

I am getting an error message KeyError: 0.73780000000000001 (this is the value of current radius ... see below) , when I try to use value of radius as a key in dictionary: The code looks somehow like this:

for n in range(len(myarray)):
    radius = (np.sqrt(x[n]**2 + y[n]**2))

The values of x and y are floats with four decimals like eg:

-25.9166 71.0444

I tried to cut the decimals with:

for n in range(len(myarray)):
    radius = (int(np.sqrt(x[n]**2 + y[n]**2))*10000)/10000.0

I don't understand why it gives me back this strange value 0.73780000000000001

David Hammen
  • 32,454
  • 9
  • 60
  • 108
user3041107
  • 123
  • 2
  • 13
  • 5
    Keying on floats is not a good idea because of accumulating rounding errors. See [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). – Frédéric Hamidi Feb 28 '14 at 09:01
  • thanks, but I'm not a computer scientist I only use python for data analysis. Do you know how could I solve this problem ? – user3041107 Feb 28 '14 at 09:09
  • 1
    Can you give us more context about what you're trying to achieve, and for what purpose you are maintaining a dictionary keyed on radii? There may be a better way to solve your problem. – Frédéric Hamidi Feb 28 '14 at 09:12
  • this value `0.73780000000000001` mustn't be produced by `-25.9166 71.0444`. you can `print x[n], y[n], radius` in your for-loop to debug your self. – zhangxaochen Feb 28 '14 at 09:13
  • If your two inputs are guaranteed to be to 4 decimal points of precision, just round your output to 8 digits and you're done. – Chris Arena Feb 28 '14 at 09:22
  • no, the x, y values were only an example and they are not 4 decimal floating points as I said ... it was only the way of printing them. I think I have to think of different way how to process the data, because with this keying with floats it's quite difficult. The problem is complicated to explain from the beginning, but thanks anyway for comments! – user3041107 Feb 28 '14 at 09:25

1 Answers1

3

first of all, I don't really understand your question:

>>> (np.sqrt(x**2 + y**2))
75.623917690899873
>>> (int(np.sqrt(x**2 + y**2))*10000)/10000.0
75.0

secondly, as @frederic-hamidi tells, you should learn more about the floating points arithmetics, also known as IEEE754 numbers, which are built that way:

IEEE754 rep

which is the build of a binary based "scientific notation". The nice thing about that is that it enables a really wide range of expression of floats, the bad thing is that you may encounter approximations that feels unnatural, but because it's making sense in binary exponent notation, and not in decimal representation. Also, you might get values like NaN (Not a Number) or infinity.

Though the calculations you gave us do not give 0.73780000000000001, that number is exactly the kind of approximation you can get with IEEE754 calculation. (Though I won't get into how one might get it)

In the end you shall not use a float as a key. Instead, use the integer representation you can get using:

>>> (int(np.sqrt(x**2 + y**2))*10000)/10000
75

by removing the .0 on the last fraction, so you do not promote the left hand side integer to float.

Community
  • 1
  • 1
zmo
  • 24,463
  • 4
  • 54
  • 90