0

So I'm trying do detect a particular circle in an image, I'm using an openCV function, I tried tweaking the parameters but I'm not happy.

So I'm going to bruteforce the 4 parameters.

To do this I wrote this piece of code wich generates a list containing all the values to try on one parameter :

def paramlistgene(increment, fromvalue, tovalue):
    paramlist = [fromvalue]
    while paramlist[-1] < tovalue:
        paramlist.append(paramlist[-1] + increment)
    return paramlist

for example paramlistgene(0.1, 0.1, 20) should generate :

[0.1, 0.2, 0.3, 0.4, 0.5, ..., 20]

but it generates this :

[0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7, 0.7999999999999999, 0.8999999999999999, 0.9999999999999999, 1.0999999999999999, 1.2, ...]

Why does this happens?

I tried print 0.1+0.1+0.1 and it does print 0.3.

Thanks

sliders_alpha
  • 2,276
  • 4
  • 33
  • 52
  • It sounds like a problem with precision error propagation. – Right leg Nov 12 '16 at 15:22
  • try `0.1 + 0.2 == 0.3` and you get `False` . `float` can't keep all real values so it keeps approximations. Sometimes it displays full value and sometimes it displays rounded value. See `print("{:0.54f}".format(0.3))` and `print("{:0.54f}".format(0.1+0.2))` – furas Nov 12 '16 at 15:24
  • oh well, I had no idea about this, I could have made such monstruous bugs! – sliders_alpha Nov 12 '16 at 15:51

0 Answers0