<p dir="ltr">Interesting piece of software) I'll try to check later if 'which' array index you are getting (in quite interesting way, using floating point math, as I see) is correct. I think that calculating array index with floating point arithmetic is wrong. Imagine what can happen if you have an array of three elements and result of floating point operations will be 3.0000001 instead of 2.999999 due to floating point limited precision. You will get wrong array index (3 instead of 2) and so your result will be wrong.<br>
Try to add a debugging conditional if 'which' is out of range. This will help more than wasting your time on valgrind.</p>